Leetcode–Binary Tree Postorder Traversal

The Problem:

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

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [3,2,1].

Recursive solution is trivial, could you do it iteratively?

Notes:

  1. The recursive method is straightforward,see solution I.
  2. However this problem requires an iterative method. The key is to first traverse down the tree to find left most leaf, and traverse up to build the result by post order. So we need a stack to trace the nodes, and a flag to check whether we are going up or down the tree.

Java Solution I recursive

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> result;
    public List<Integer> postorderTraversal(TreeNode root) {
        result= new ArrayList<Integer>();
        post(root);
        return result;
    }
    
    private void post(TreeNode root){
        if(root==null)
            return;
        post(root.left);
        post(root.right);
        result.add(root.val);
    }
}

Java Solution II iterative

/**
 * Definition for binary tree
 * 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> result =  new ArrayList<Integer>();
        Stack<TreeNode> st = new Stack<TreeNode>();
        if(root==null) return result;
        st.push(root);
        TreeNode pre = null;
        while(!st.empty()){
            TreeNode top = st.peek();
            if(pre==null || pre.left == top || pre.right ==top){
                // down the tree
                if(top.left!= null){
                    st.push(top.left);
                }
                else if(top.right!= null){ 
                    // if left exist, don't push right, that will be done when going up 
                    st.push(top.right);
                }
                else{ //reach a leaf 
                    result.add(st.pop().val);
                }
            }
            else if(top.left == pre && top.right!= null){
                    st.push(top.right);
            }
            else{
                result.add(st.pop().val);
            }
            pre = top;
        }
        return result;
    }
}
Advertisements

2 thoughts on “Leetcode–Binary Tree Postorder Traversal

  1. Pingback: Leetcode–Binary Tree Preorder Traversal | Linchi is coding

  2. Pingback: Linchi is coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s