Binary Tree Preorder Traversal

1. Recursive:

Very Straight forward, If the node is not null, add root -> traversal left -> traversal right

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Preorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> rst = new ArrayList<Integer>();
        traversal(rst, root);
        return rst;
    }
    
    private void traversal(ArrayList<Integer> rst, TreeNode root){
        if (root == null) {
            return;
        }
        rst.add(root.val);
        traversal(rst, root.left);
        traversal(rst, root.right);
    }
}

 

2. Iterator(find a very good explanation  https://sites.google.com/site/jennyshelloworld/company-blog/chapter-3---binary-tree-divide-conquer):

For tree problems, every recursive solution usually has an iterative solution, which uses Stack to operate inner memory mechanism of recursion. Here we can define a stack and do below things:

1. Add root to the stack, we will pop it later, to expand the tree up side down

2. Pop the node, add it to the result and use a temp TreeNode to do the next expand

3. Look at the node and find whether it has right child, if yes, put the right child in the stack

4. If the current node does not have a right child, try to find whether it has a left child, if yes, put the left child in the stack

5. Do things until the stack is empty and return the rest

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Preorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> rst = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        
        if (root == null) {
            return rst;
        } 
        
        stack.push(root);
        while (!stack.empty()) {
            TreeNode node = stack.pop();
            rst.add(node.val);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
        return rst;
    }
}

 

3. Divide and Conquer

Note:

Divide: divide to left tree and right tree

Conquer: Add the root -> left -> right

Explanation:

The node knows the left and right ArrayList and then add its root, left, right. 每一层都按先把腿都一点点组装好, 最后一步先放脑袋,然后把腿按顺序插进去。最后装的是整个list的root。 所以root会在最前面。

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Preorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> rst = new ArrayList<Integer>();
        if (root == null) {
            return rst;
        }
        
        ArrayList<Integer> left = preorderTraversal(root.left);
        ArrayList<Integer> right = preorderTraversal(root.right);
        
        //System.out.println(root.val);
        rst.add(root.val);
        rst.addAll(left);
        rst.addAll(right);
        return rst;
    }
}

 

posted on 2017-03-12 01:30  codingEskimo  阅读(100)  评论(0编辑  收藏  举报

导航