mediumTreesAmazonMicrosoftBloomberg
Blueprint Reconstructor
An archaeology lab holds two separate traversal logs from a lost ancestral family tree. Reconstructing the full genealogy requires splicing preorder (ancestry priority) with inorder (left-to-right chronology).
Given two integer arrays preorder and inorder — the preorder and inorder traversals of the same binary tree — construct and return the binary tree.
Choose approach
3
active
1/11
Narrationstep 1 of 11
Take preorder[0] = 3 as subtree root for inorder[0..4].
Variables
pre=3
lo=0
hi=4
p=0
How to think about it
- First preorder value is always the root of its subtree.
- Index map from inorder enables O(1) split into left-size and right-size ranges.
- Recurse into left range first (consumes preorder before right).
Edge cases to catch in interview
- Duplicates in input are forbidden by the problem.
- Single node -> directly maps.
Examples
Example 1
[3,9,20,null,null,15,7]{"preorder":[3,9,20,15,7],"inorder":[9,3,15,20,7]}
Example 2
[-1]{"preorder":[-1],"inorder":[-1]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed