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.
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.