π³105. Construct Binary Tree from Preorder and Inorder Traversal
Grind 75 | Medium | Monday 10th April, 2023
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.
Constraints:
1 <= preorder.length <= 3000inorder.length == preorder.length-3000 <= preorder[i], inorder[i] <= 3000preorderandinorderconsist of unique values.Each value of
inorderalso appears inpreorder.preorderis guaranteed to be the preorder traversal of the tree.inorderis guaranteed to be the inorder traversal of the tree.

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]Input: preorder = [-1], inorder = [-1]
Output: [-1]SOLUTION
The order in which you visit nodes for each traversal. Preorder: root node -> left subtree -> right subtree Inorder: left subtree -> root node -> right subtree
To reconstruct a binary tree from its preorder and inorder traversals, we can start by identifying the root node. Note that the root node is always the first node in the preorder list. We then find the index of the root node in the inorder list. This tells us the boundaries of the left and right subtrees for the root node. All the nodes to the left of the root node belong to the left subtree, and all the nodes to the right of the root node belong to the right subtree.
We iterate over the preorder list and pop each element until there are no more elements. For each popped element, we find its index in the inorder list, which tells us the boundaries of the left and right subtrees for the current element. We then recursively build the left and right subtrees for the current element. To do this, we pass the preorder list and a sublist of the inorder list that corresponds to the current subtree to the recursive function. By recursively building the left and right subtrees for each element in the preorder list, we can reconstruct the entire binary tree.
TIME AND SPACE COMPLEXITY
TIME: O(n^2), where n is the number of nodes in the binary tree. This is because of the inorder.index() method used to find the index of the root node in the in-order traversal takes O(n) time in the worst case. The recurse function is called once for each node in the binary tree, and each call takes O(n) time to search for the inorder index. Therefore, the overall time complexity is O(n^2).
SPACE: O(n), where n is the number of nodes in the binary tree. We build a tree with n nodes.
***written with help from ChatGPT
Last updated