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  阅读(107)  评论(0)    收藏  举报

编辑推荐:
· 一则复杂 SQL 改写后有感
· golang中写个字符串遍历谁不会?且看我如何提升 50 倍
· C# 代码如何影响 CPU 缓存速度?
· 智能桌面机器人:使用 .NET 为树莓派开发 Wifi 配网功能
· C# 模式匹配全解:原理、用法与易错点
阅读排行:
· 时隔半年,拾笔分享:来自一个大龄程序员的迷茫自问
· 《程序员的底层思维》读后感
· 曾经风光无限的 Oracle DBA 已经落伍了吗?
· 不写一行代码 .NET 使用 FluentCMS 快速构建现代化内容管理系统(CMS)
· C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock

导航

< 2025年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

统计

点击右上角即可分享
微信分享提示