Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note: You may assume that duplicates do not exist in the tree.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]Return the following binary tree:
3
/ \
9 20
/ \
15 7Analysis
关键在于Preorder中[root, left, right]的遍历顺序和Inorder的[left, root, right]顺序。因此可以从preorder中得到root,在inorder中找到对应的root,于是left和right子树就可以分别进行进一步查找。
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null || preorder.length != inorder.length) {
return null;
}
return buildTreeHelper(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1) ;
}
TreeNode buildTreeHelper(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd) {
if (inStart > inEnd) {
return null;
}
int rootPosition = findPosition(inStart, inEnd, inorder, preorder[preStart]);
TreeNode root = new TreeNode(preorder[preStart]);
root.left = buildTreeHelper(preorder, inorder, preStart + 1, preStart + rootPosition - inStart, inStart, rootPosition - 1 );
root.right = buildTreeHelper(preorder, inorder, preEnd - inEnd + rootPosition + 1, preEnd, rootPosition + 1, inEnd );
return root;
}
int findPosition (int start, int end, int[] arr, int target) {
int i;
for (i = start; i <= end; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
}Using Pre-built Hashmap for Index Lookup
via @yavinci: use HashMap to cache the inorder[] position.
*LeetCode Official Solution
Depth First Search (DFS) - Recursion - ID Map
Reference
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/solution/
Last updated
Was this helpful?