Reconstruct a tree from two traversals
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: Reconstruct a tree from two traversals evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- All node values are unique.
- 0 <= n <= 10^4 where n = len(preorder) = len(inorder) for a valid tree.
- preorder and inorder must contain the same multiset of values to form a valid tree; otherwise return 'INVALID'.
- If the two arrays differ in length, return 'INVALID'.
- For an empty tree (both arrays empty), return None.
Examples
Input: ([3,9,20,15,7], [9,3,15,20,7])
Expected Output: [3, 9, 20, None, None, 15, 7]
Explanation: Typical balanced-ish tree: root 3 has left child 9 and right child 20; 20's children are 15 and 7.
Input: ([-1], [-1])
Expected Output: [-1]
Explanation: Single-node tree with a negative value — just the root.
Input: ([], [])
Expected Output: None
Explanation: Empty tree: both traversals are empty, so there is no root.
Input: ([1,2,3], [3,2,1])
Expected Output: [1, 2, None, 3]
Explanation: Left-skewed (degenerate) tree: 1's left is 2, 2's left is 3; no right children.
Input: ([1,2,3], [1,2,3])
Expected Output: [1, None, 2, None, 3]
Explanation: Right-skewed (degenerate) tree: each node has only a right child.
Input: ([1,2], [1,2,3])
Expected Output: INVALID
Explanation: Mismatched array lengths cannot form a valid tree — return the INVALID sentinel.
Input: ([1,2,3], [4,5,6])
Expected Output: INVALID
Explanation: Same length but disjoint value sets cannot correspond to traversals of one tree — return INVALID.
Hints
- The first element of preorder is always the root of the (sub)tree.
- Find the root's position in inorder: everything to its left is the left subtree, everything to its right is the right subtree.
- Precompute a value->index hash map of inorder so locating each root is O(1) instead of O(n), giving overall O(n) time.
- Walk preorder with a single shared cursor (left-to-right) while recursing into left then right inorder ranges — this naturally consumes roots in preorder order.
- Validate first: if lengths differ or the value sets differ, the input cannot form a valid tree; return the 'INVALID' sentinel.