5.30 Tree Traversal + Tree manipulation

Posted AugusKong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5.30 Tree Traversal + Tree manipulation相关的知识,希望对你有一定的参考价值。

    1. Binary Tree Preorder Traversal

题目:对一棵二叉树进行前序遍历,并将结果存在一个List

public class Solution {
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        // null or leaf
        if (root == null) {
            return result;
        }

        // Divide
        ArrayList<Integer> left = preorderTraversal(root.left);
        ArrayList<Integer> right = preorderTraversal(root.right);

        // Conquer
        result.add(root.val);
        result.addAll(left);
        result.addAll(right);
        return result;
    }
}

以上是关于5.30 Tree Traversal + Tree manipulation的主要内容,如果未能解决你的问题,请参考以下文章

Construct Binary Tree from Preorder and Inorder Traversal

Binary Tree Traversal In Three Ways In Leetcode

(Tree)94.Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

LeetCode145. Binary Tree Postorder Traversal 解题报告