Detect Whether One Org Chart Subtree Appears Inside Another
Last updated: November 29, 2026
Quick Overview
Given the roots of two binary trees, big and small, return whether small appears in big as a subtree: some node of big whose entire descendant structure and values match small exactly.
Meta
November 29, 20260
0
0 solved
Given the roots of two binary trees, big and small, return whether small appears in big as a subtree: some node of big whose entire descendant structure and values match small exactly.
A Meta phone screen tree question where the interviewer checks that you separate the 'same tree' check from the 'search every node' loop, and then asks how to beat O(n * m) with serialization or hashing.
What the Interviewer Expects
- Write a same-tree helper
- Try it at every node of the larger tree
- Know that a match must include all descendants, not a prefix
- Discuss an O(n + m) version via serialization with null markers
- Handle an empty small tree
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
- How does serializing with null markers let you use substring search?
- How would Merkle-style hashing of subtrees help across many queries?
- What if small only needs to match a top portion of a subtree?
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 binary tree roots big and small. Return True if some node x in big has a subtree identical to small in both shape and values, including where leaves end. Treat an empty `small...
High-Level Design
Two trees are the same if both are None, or both exist with equal values and their left and right subtrees are the same. Walk every node of big and run that check against small.
Worst case is O(n...