首页 > 其他 > 详细

lintcode-easy-Binary Tree Postorder Traversal

时间:2016-02-21 18:23:41      阅读:170      评论:0      收藏:0      [点我收藏+]

Given a binary tree, return the postorder traversal of its nodes‘ values.

 

Example

Given binary tree {1,#,2,3},

   1
         2
    /
   3

 

return [3,2,1].

Challenge

Can you do it without recursion?

 

还是两种方法:recursive, iterative

 

1. recursive

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        // write your code here
        
        ArrayList<Integer> result = new ArrayList<Integer>();
        
        if(root == null)
            return result;
        
        helper(root, result);
        
        return result;
    }
    
    public void helper(TreeNode root, ArrayList<Integer> result){
        if(root == null){
            return;
        }
        else{
            helper(root.left, result);
            helper(root.right, result);
            result.add(root.val);
            return;
        }
    }
    
}

2. iterative

与中序遍历不同之处在于,要判断当前node和上一个node之间的关系才能得到正确的结果(也可以只使用一个reference和一个stack)。

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<Integer>();
        
        if(root == null)
            return result;
        
        TreeNode p = root;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(p);
        
        TreeNode prev = null;
        TreeNode curr = null;
        
        while(!stack.isEmpty()){
            curr = stack.peek();
            
            if(prev == null || prev.left == curr || prev.right == curr){
                if(curr.left != null){
                    stack.push(curr.left);
                }
                else if(curr.right != null){
                    stack.push(curr.right);
                }
                else{
                    stack.pop();
                    result.add(curr.val);
                }
            }
            else if(curr.left == prev){
                if(curr.right != null){
                    stack.push(curr.right);
                }
                else{
                    stack.pop();
                    result.add(curr.val);
                }
            }
            else if(curr.right == prev){
                stack.pop();
                result.add(curr.val);
            }
            
            prev = curr;
        }
        
        return result;
    }
}

 

lintcode-easy-Binary Tree Postorder Traversal

原文:http://www.cnblogs.com/goblinengineer/p/5205289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!