Leetcode–Binary Tree Preorder Traversal

The Problem:

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

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

   1
    \
     2
    /
   3

return [1,2,3].

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.

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> preorderTraversal(TreeNode root) {
        result = new ArrayList<Integer>();
        if(root==null) return result;
        pre(root);
        return result;
    }
    private void pre(TreeNode root){
        result.add(root.val);
        if(root.left!= null)
            pre(root.left);
        if(root.right!=null)
            pre(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> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        if(root==null) return result;
        Stack<TreeNode> st = new Stack<TreeNode>();
        st.push(root);
        TreeNode pre = null;
        while(!st.isEmpty()){
            TreeNode top = st.peek();
            if(pre==null || pre.left == top || pre.right==top){
                // down
                result.add(top.val);
                if(top.left!= null){
                    st.push(top.left);
                }
                else if(top.right != null){
                    st.push(top.right);
                }
                else{
                    // leaf node
                    st.pop();
                }
            }
            else if(top.left == pre && top.right!= null){
                // up
                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