Rebuild a Visual Tree From Preorder and Inorder Dumps
Last updated: November 29, 2026
Quick Overview
A crash dump of a WPF visual tree recorded node IDs in preorder and in inorder, all unique. Reconstruct the original binary tree.
Microsoft
November 29, 20260
0
0 solved
A crash dump of a WPF visual tree recorded node IDs in preorder and in inorder, all unique. Reconstruct the original binary tree.
A construction problem that shows whether a candidate understands what each traversal encodes. Microsoft interviewers expect the hash map for inorder positions and an O(n) solution, not the O(n^2) slicing version.
What the Interviewer Expects
- Use preorder's first element as the root
- Split inorder at the root's index to size the subtrees
- Store inorder indices in a dict for O(1) lookup
- Avoid list slicing, which copies and adds O(n^2)
- Explain why unique values are required
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
- Rebuild from postorder and inorder instead.
- Can preorder and postorder alone determine the tree? When?
- Values may repeat. What extra information would you need?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Two lists of the same length with unique values.
- Output: root of the reconstructed tree.
- Input is assumed valid....
High-Level Design
Preorder lists root, then the whole left subtree, then the whole right subtree. Inorder lists left subtree, root, right subtree. So the next preorder value is the root of the current subtree, and its ...