Leetcode–Binary Tree Inorder Traversal

The Problem:

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

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

   1
    \
     2
    /
   3

return [1,3,2].

Recursive solution is trivial, could you do it iteratively?

Notes:

  1. The recursive method is straightforward,see solution I.
  2. For iterative solution, similar to Binary Tree Postorder Traversal, traverse down to get left most leaf and then up, and check the relationship between previous node and current node to see if it’s going up or down. Note that no matter it is inorder/preorder/postorder, in a DFS, should always use Stack, in a BFS, should use Queue.
  3. Be careful about when to add to result and when to pop/push.

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> inorderTraversal(TreeNode root) {
        result = new ArrayList<Integer>();
        if(root==null) return result;
        inorder(root);
        return result;
    }
    private void inorder(TreeNode root){
        if(root.left!= null)
            inorder(root.left);
        result.add(root.val);
        if(root.right!=null)
            inorder(root.right);
    }
}

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> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        if(root==null) return result;
        Stack<TreeNode> st = new Stack<TreeNode>();
        st.add(root);
        TreeNode pre = null;
        while(!st.empty()){
            TreeNode top = st.peek();
            if(pre==null || pre.left ==top || pre.right==top){
                // down
                if(top.left!= null){
                    st.push(top.left);
                }
                else if(top.right!= null){
                    // record parent before go to right child
                    result.add(top.val);
                    st.push(top.right);
                }
                else{
                    result.add(st.pop().val);
                }
            }
            else{
                if(top.left ==pre){
                    // record parent before go to right
                    result.add(top.val);
                    if(top.right!= null)
                        st.push(top.right);
                }
                else{
                    st.pop();
                }
            }
            pre=top;
        }
        return result;
    }
}
Advertisements

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