Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
How does preorder[] array helps?
The first element in preorder[] acts as the root of the tree (in this case, 1).
How does inorder[] array helps?
inorder[] helps determine the left and right subtrees of the root. Elements appearing before the root in inorder[] belong to the left subtree, while elements appearing after belong to the right subtree. In our case, it is [4, 2, 5] on left and [6, 3, 7] on right. (See picture below).
See this step above and recursively construct left subtree and link it root.left and recursively construct right subtree and link it root.right.
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder ==null|| preorder.length== 0 || inorder.length== 0 || preLength != inLength) {
returnnull;
}
int preLength = preorder.length;
int inLength = inorder.length;
return helper(preorder, inorder, 0, 0, inLength - 1);
}
privatestatic TreeNode helper(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) {
if (inStart > inEnd || preStart > preorder.length- 1) {
returnnull;
}
// Pick current node from Preorder traversal using preStart and increment preStart TreeNode root =new TreeNode(preorder[preStart]);
// If this node has no children then returnif (inStart == inEnd) {
return root;
}
// Else find the index of this node in Inorder traversalint inIndex = linearSearch(inorder, root.val, inStart, inEnd);
// Using index in Inorder traversal, construct left and right subtrees root.left= helper(preorder, inorder, preStart + 1, inStart, inIndex - 1);
// We are skipping the elements on the left of the current pre order node by adding inIndex - inStart + 1 root.right= helper(preorder, inorder, preStart + inIndex - inStart + 1, inIndex + 1, inEnd);
return root;
}
staticintlinearSearch(int[] arr, int value, int start, int end) {
for (int i = start; i <= end; i++) {
if (arr[i]== value) {
return i;
}
}
return-1;
}