Question Definition
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Java Solution
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeHelper(preorder, inorder, 0, preorder.length - 1, 0);
}
public TreeNode buildTreeHelper(int[] preorder, int[] inorder, int start, int end, int index) {
if(start > end || index >= preorder.length)
return null;
int rootVal = preorder[index];
TreeNode root = new TreeNode(rootVal);
int rootIndex = start;
for(; rootIndex <= end; rootIndex++)
if(inorder[rootIndex] == rootVal)
break;
root.left = buildTreeHelper(preorder, inorder, start, rootIndex - 1, index + 1);
root.right = buildTreeHelper(preorder, inorder, rootIndex + 1, end, index + rootIndex - start + 1);
return root;
}
Comments