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
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
binary trees
tree construction
divide and conquer
hash map
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported