[lintcode easy]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal

 

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

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

 

return [3,2,1].

Challenge

Can you do it without recursion?

Among preorder,inorder and postorder binary tree traversal problems, postorder traversal is the most complicated one.

Analysis

The key to to iterative postorder traversal is the following:

  1. The order of "Postorder" is: left child -> right child -> parent node.
  2. Find the relation between the previously visited node and the current node
  3. Use a stack to track nodes

As we go down the tree to the lft, check the previously visited node. If the current node is the left or right child of the previous node, then keep going down the tree, and add left/right node to stack when applicable. When there is no children for current node, i.e., the current node is a leaf, pop it from the stack. Then the previous node become to be under the current node for next loop. You can using an example to walk through the code.

/**
 * 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> list=new ArrayList<Integer>();
        if(root==null) return list;
        
        Stack<TreeNode> stack=new Stack<TreeNode>();
        stack.push(root);
        
        TreeNode prev=null;
        while(!stack.empty())
        {
            // go down the tree.
            //check if current node is leaf, if so, process it and pop stack,
            //otherwise, keep going down

            TreeNode cur=stack.peek();
            if(prev==null||prev.left==cur||prev.right==cur)
            {
                if(cur.left!=null)
                {
                    stack.push(cur.left);
                }
                else if(cur.right!=null)
                {
                    stack.push(cur.right);
                }
                else
                {
                    stack.pop();
                    list.add(cur.val);
                }
                
            }
             //go up the tree from left node    
            //need to check if there is a right child
            //if yes, push it to stack
            //otherwise, process parent and pop stack

            else if(cur.left==prev)
            {
                if(cur.right!=null)
                {
                    stack.push(cur.right);
                }
                else
                {
                    stack.pop();
                    list.add(cur.val);
                }
            }
            //go up the tree from right node 
            //after coming back from right node, process parent node and pop stack. 
            else if(cur.right==prev)
            {
                stack.pop();
                list.add(cur.val);
            }
            prev=cur;
           
        }
        return list;
    }
}

文章来源:http://www.programcreek.com/2012/12/leetcode-solution-of-binary-tree-inorder-traversal-in-java/

posted on 2015-11-17 04:02  一心一念  阅读(119)  评论(0编辑  收藏  举报

导航