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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值