二叉树的迭代遍历

二叉树的迭代遍历

后序遍历

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>(); 
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();   
        stack.push(root);  
        while (!stack.isEmpty()) {
            TreeNode t = stack.pop();   
            if (t != null) {   
                stack.push(t);   
                stack.push(null);  
                if (t.right != null) stack.push(t.right);  
                if (t.left != null)  stack.push(t.left); 
            } else {  
                res.add(stack.pop().val);   
            }
        }
        return res;
    }
}

前序遍历

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>(); 
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();   
        stack.push(root);  
        while (!stack.isEmpty()) {
            TreeNode t = stack.pop();   
            if (t != null) {   
                if (t.right != null) stack.push(t.right);  //
                if (t.left != null)  stack.push(t.left); //
                stack.push(t);   //
                stack.push(null);  //
            } else {  
                res.add(stack.pop().val);   
            }
        }
        return res;
    }
}

中序遍历

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>(); 
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();   
        stack.push(root);  
        while (!stack.isEmpty()) {
            TreeNode t = stack.pop();   
            if (t != null) {  
                if (t.right != null) stack.push(t.right);   
                stack.push(t);   
                stack.push(null);   
                if (t.left != null)  stack.push(t.left); 
            } else {  
                res.add(stack.pop().val);   
            }
        }
        return res;
    }
}
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        Deque<TreeNode> stk = new LinkedList<TreeNode>();
        while (root != null || !stk.isEmpty()) {
            while (root != null) {
                stk.push(root);
                root = root.left;
            }
            root = stk.pop();
            res.add(root.val);
            root = root.right;
        }
        return res;
    }
}

层次遍历

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res=new ArrayList<List<Integer>>();
        if(root==null) return res;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> list=new ArrayList<>();
            int size=queue.size();
            for(int i=0;i<size;i++){
                TreeNode node=queue.poll();
                list.add(node.val);
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
            }
            res.add(list);
        }
        return res;
    }
}

未经作者同意请勿转载

本文来自博客园作者:aixueforever,原文链接:https://www.cnblogs.com/aslanvon/p/13631838.html

原文地址:https://www.cnblogs.com/aslanvon/p/13631838.html