By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(Complete Guide for FAANG-Level Interviews)
"This problem appears in 1 out of every 3 onsite interviews—master it, and you prove you can recursively decompose problems, optimize with hash maps, and handle edge cases like a senior engineer."
Before tackling this problem, ensure you understand: 1. Binary Tree Traversals (Preorder, Inorder, Postorder) – Know the order of node visits. 2. Hash Maps (Dictionaries) – For O(1) lookups of node positions. 3. Recursive Problem Decomposition – Breaking problems into smaller subproblems (divide and conquer).
[3,9,20,15,7]
[9,3,15,20,7]
inorder
left
right
(Repeatable mental checklist for every problem of this type.)
The first element in preorder is always the root of the tree (or subtree).
Locate the Root in Inorder
All elements right of root in inorder = right subtree.
Recursively Build Subtrees
left_size
Right Subtree:
Optimize with Bounds (Avoid Array Slicing)
Instead of slicing arrays (O(n) space), use pointers (pre_start, in_start, in_end) to track ranges.
pre_start
in_start
in_end
Base Case
If in_start > in_end, return None (no subtree exists).
in_start > in_end
None
Edge Cases
Problem: Given preorder = [3,9,20,15,7], inorder = [9,3,15,20,7], construct the binary tree.
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Thought Process: 1. Root = 3 (first in preorder). 2. In inorder, 3 is at index 1 → Left subtree: [9], Right subtree: [15,20,7]. 3. Left Subtree: - Preorder: [9] (next element after root). - Inorder: [9] → Root = 9, no children. 4. Right Subtree: - Preorder: [20,15,7]. - Inorder: [15,20,7] → Root = 20, Left = [15], Right = [7]. 5. Recurse: - Left of 20: [15] → Leaf node. - Right of 20: [7] → Leaf node.
3
1
[9]
[15,20,7]
9
[20,15,7]
20
[15]
[7]
Code (Python):
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildTree(preorder, inorder): inorder_map = {val: idx for idx, val in enumerate(inorder)} def helper(pre_start, in_start, in_end): if in_start > in_end: return None root_val = preorder[pre_start] root = TreeNode(root_val) root_pos = inorder_map[root_val] left_size = root_pos - in_start root.left = helper(pre_start + 1, in_start, root_pos - 1) root.right = helper(pre_start + left_size + 1, root_pos + 1, in_end) return root return helper(0, 0, len(inorder) - 1)
Time Complexity: O(n) (each node processed once). Space Complexity: O(n) (hash map + recursion stack).
Why This Works: - Preorder gives the root first, inorder splits left/right subtrees. - Hash map avoids O(n) searches for root positions.
Problem: What if inorder has duplicates? (e.g., preorder = [1,1], inorder = [1,1])
preorder = [1,1]
inorder = [1,1]
Thought Process: - Problem is unsolvable if duplicates exist (ambiguous root position). - Assumption: All values are unique (standard interview constraint).
Code Adjustment: - Add a check at the start:
if len(set(inorder)) != len(inorder): return None # or raise ValueError
Why This Works: - Without unique values, we cannot determine the root’s position in inorder.
Problem: Given postorder and inorder, construct the tree.
postorder
Thought Process: 1. Root = last element in postorder (not first). 2. Inorder splits left/right subtrees (same as before). 3. Recurse: - Left subtree: post_start to post_start + left_size - 1. - Right subtree: post_start + left_size to post_end - 1.
post_start
post_start + left_size - 1
post_start + left_size
post_end - 1
def buildTree(postorder, inorder): inorder_map = {val: idx for idx, val in enumerate(inorder)} def helper(post_end, in_start, in_end): if in_start > in_end: return None root_val = postorder[post_end] root = TreeNode(root_val) root_pos = inorder_map[root_val] left_size = root_pos - in_start root.left = helper(post_end - (in_end - root_pos) - 1, in_start, root_pos - 1) root.right = helper(post_end - 1, root_pos + 1, in_end) return root return helper(len(postorder) - 1, 0, len(inorder) - 1)
Time Complexity: O(n). Space Complexity: O(n).
Why This Works: - Postorder’s root is at the end, but the logic for splitting subtrees remains the same.
left_size = root_pos - in_start
root_pos - 1
if in_start > in_end: return None
"Here’s the playbook to crush this problem in your interview: 1. Root is first in preorder—grab it, then find its position in inorder. 2. Split inorder into left and right subtrees around the root. 3. Recurse on the left and right, using pointers to avoid slicing arrays. 4. Optimize with a hash map for O(1) root lookups. 5. Edge cases: Empty input, duplicates (clarify with interviewer).
This pattern shows up in Google, Meta, and Amazon interviews—practice it until you can code it in your sleep. Good luck!
postorder + inorder
preorder + postorder
You’ve got this! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.