leetcode 二叉树遍历非递归版本

pre order和in order的思路相似,通过current==null来判断是入栈还是出栈。
postorder需要记录prev和current的关系:

preordrer
[code]

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<Integer>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        TreeNode current=root;
        list.add(root.val);
        stack.push(root);
        while(stack.size()>0)
        {
            if(current==null)
            {
                current=stack.pop();
                if(current.right!=null)
                {
                    stack.push(current.right);
                    list.add(current.right.val);
                    current=current.right;
                }
                else current=null;
            }
            else if(current.left!=null)
            {
                stack.push(current.left);
                list.add(current.left.val);
                current=current.left;
            }
            else
            {
                stack.pop();
                if(current.right!=null)
                {
                    stack.push(current.right);
                    list.add(current.right.val);
                    current=current.right;
                }
                else current=null;
            }

        }
        return list;
    }
}

inorder
[code]

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<Integer>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        stack.push(root);
        TreeNode current=root,prev=null;
        while(stack.size()>0)
        {
            if(current==null)
            {
                current=stack.pop();
                list.add(current.val);
                if(current.right!=null)
                {
                    stack.push(current.right);
                    current=current.right;
                }
                else current=null;
            }
            else if(current.left!=null)
            {
                stack.push(current.left);
                current=current.left;
            }
            else
            {
                stack.pop();
                list.add(current.val);
                if(current.right!=null)
                {
                    stack.push(current.right);
                    current=current.right;
                }
                else current=null;
            }
        }
        return list;
    }
}

post order
[我自己的code]

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<Integer>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<TreeNode>();
        stack.push(root);
        TreeNode current=root, prev=null;
        while(stack.size()>0)
        {
            if(current==null)current=stack.peek();
            if(prev!=null && prev==current.left)
            {
                if(current.right!=null)
                {
                    stack.push(current.right);
                    current=current.right;
                }
                else
                {
                    list.add(current.val);
                    stack.pop();
                    prev=current;
                    current=null;
                }
            }
            else if(prev!=null && prev==current.right)
            {
                list.add(current.val);
                stack.pop();
                prev=current;
                current=null;
            }
            else
            {
                if(current.left!=null)
                {
                    stack.push(current.left);
                    current=current.left;
                }
                else if(current.right!=null)
                {
                    stack.push(current.right);
                    current=current.right;
                }
                else
                {
                    list.add(current.val);
                    stack.pop();
                    prev=current;
                    current=null;
                }
            }
        }
        return list;
    }
}

[九章算法的答案]思路差不多

//Iterative
public ArrayList<Integer> postorderTraversal(TreeNode root) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode prev = null; // previously traversed node
    TreeNode curr = root;

    if (root == null) {
        return result;
    }

    stack.push(root);
    while (!stack.empty()) {
        curr = stack.peek();
        if (prev == null || prev.left == curr || prev.right == curr) { // traverse down the tree
            if (curr.left != null) {
                stack.push(curr.left);
            } else if (curr.right != null) {
                stack.push(curr.right);
            }
        } else if (curr.left == prev) { // traverse up the tree from the left
            if (curr.right != null) {
                stack.push(curr.right);
            }
        } else { // traverse up the tree from the right
            result.add(curr.val);
            stack.pop();
        }
        prev = curr;
    }

    return result;
}

你可能感兴趣的:(leetcode,leetcode,tree)