day 14 递归遍历 迭代遍历

递归遍历

链接:代码随想录

前序:

class Solution {
    public List preorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        preorder(root, res);
        return res;


    }
    public void preorder(TreeNode root, List res){
        if(root==null){
            return;
        }
        res.add(root.val);
        preorder(root.left, res);
        preorder(root.right,res);
    }
}

后序:

class Solution {
    public List postorderTraversal(TreeNode root) {
        List  list = new ArrayList<>();
        postOrder(root, list);
        return list;

    }
    public void postOrder(TreeNode root, List res){
        if(root == null){
            return ;
        }
        postOrder(root.left, res);
        postOrder(root.right, res);
        res.add(root.val);
    }
}

中序:

class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        InOrder(root, res);
        return res;

    }
    public void InOrder(TreeNode root, List res){
        if(root == null){
            return ;
        }
        InOrder(root.left,res);
        res.add(root.val);
        InOrder(root.right,res);
    }
}

后序:

class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        InOrder(root, res);
        return res;

    }
    public void InOrder(TreeNode root, List res){
        if(root == null){
            return ;
        }
        InOrder(root.left,res);
        res.add(root.val);
        InOrder(root.right,res);
    }
}

非递归遍历;

前序遍历:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List preorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        Stack stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode x = stack.peek();
            stack.pop();
            if(x != null){
                res.add(x.val);
            }else{
                continue;
            }
            stack.push(x.right);
            stack.push(x.left);


        }
        return res;

    }
}

中序遍历:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        Stack stack = new Stack<>();
        
        TreeNode cur = root;
        while(cur!=null || !stack.isEmpty()){
            if(cur!=null){
                stack.push(cur);
                cur = cur.left;
            }else{
                TreeNode node = stack.pop();
                res.add(node.val);
                cur = node.right;
            }
        }
        return res;

    }
}

后序遍历:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List postorderTraversal(TreeNode root) {
        List res = new ArrayList<>();
        Stack stack = new Stack<>();
        if(root == null){
            return res;
        }
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode x = stack.pop();
            res.add(x.val);
            if(x.left != null){
                stack.push(x.left);
            }
            if(x.right != null){
                stack.push(x.right);
            }
        }
        Collections.reverse(res);
        return res;

    }
}

你可能感兴趣的:(leetcode,算法,数据结构)