Rebuild a Category Tree From Its Preorder and Inorder Dumps
Last updated: November 29, 2026
Quick Overview
Given the preorder and inorder traversals of a binary tree with unique node IDs, reconstruct and return the original tree.
Meta
November 29, 20260
0
0 solved
Given the preorder and inorder traversals of a binary tree with unique node IDs, reconstruct and return the original tree.
A Meta onsite tree construction problem framed around restoring a Marketplace category hierarchy from two exported traversals. The interviewer checks that you use a hash map for inorder positions to stay O(n), and that you can explain why uniqueness is required.
What the Interviewer Expects
- Take the next preorder value as the root
- Split inorder at the root's index
- Precompute inorder indices in a hash map
- Consume preorder with a moving pointer instead of slicing
- Explain why duplicates break uniqueness
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Can you rebuild from postorder and inorder?
- Why can't preorder and postorder alone determine the tree?
- How would you do it iteratively with a stack?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs are two lists pre and ino of the same length, containing the same unique values. Return the root of the binary tree whose preorder is pre and inorder is ino. Empty lists return None....
High-Level Design
Preorder lists the root first. In inorder, everything left of that root belongs to its left subtree and everything right to its right subtree. Recursively, the next preorder values build the left subt...