easyTreesGoogleAmazonFacebook
Subassembly Matcher
An industrial factory scanner analyzes modular blue-prints to determine if a smaller sub-assembly configuration exists exactly inside a larger system layout.
Given the roots of two binary trees `root` and `subRoot`, return true if there is a subtree of `root` with the same structure and node values of `subRoot`.
Choose approach
⚙
3
4
5
1
2
4
1
2
1/6
Narrationstep 1 of 6
Scan Main blueprint (Left) to locate sub-assembly template (Right).
Variables
status=Scan start
How to think about it
- Traverse main tree to find matching root values.
- When target candidates match, compare shapes node by node.
- If identity check matches completely, bubble up True.
Edge cases to catch in interview
- Sub-root empty -> always vacuously true.
- Blueprint empty but sub-root exists -> false.
Examples
Example 1
true{"mainArr":[3,4,5,1,2],"subArr":[4,1,2]}
Example 2
false{"mainArr":[3,4,5,1,2,null,null,null,null,0],"subArr":[4,1,2]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed