二叉树自己能理解一二,不算难。自己也观察出来了从Preorder里知道根结点,再在Inorder里划分左右子树,如此不断递归二分。代码抄袭了这个的:
LeetCode – Construct Binary Tree from Preorder and Inorder Traversal (Java)
From the pre-order array, we know that first element is the root. We can find the root in in-order array. Then we can identify the left and right sub-trees of the root from in-order array. Using the length of left sub-tree, we can identify left and right sub-trees in pre-order array. Recursively, we can build up the tree.
Java
1 | /** |
Submission Detail
- 203 / 203 test cases passed.
- Runtime: 13 ms
- Your runtime beats 55.29 % of java submissions.