lintcode-easy-Binary Tree Postorder Traversal

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;
    }
}

 

posted @ 2016-02-21 17:45  哥布林工程师  阅读(136)  评论(0编辑  收藏  举报