Subtree of Another Tree
An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 2
- Core for
- 4 roles
The problem
Decide whether the tree subRoot appears inside the tree root. It counts only if some node of root, taken together with everything below it, matches subRoot exactly in shape and in values.
Example 1
- Input
- root=[8,6,9,4,7], subRoot=[6,4,7]
- Output
- true
- Why
- The left child 6 with its leaves 4 and 7 is exactly subRoot.
Example 2
- Input
- root=[8,6,9,4,7,null,null,null,null,2], subRoot=[6,4,7]
- Output
- false
- Why
- The 7 inside root now carries a child 2, so no subtree matches subRoot exactly.
Example 3
- Input
- root=[1,1], subRoot=[1]
- Output
- true
- Why
- A single node tree can be a subtree of a larger tree.
Constraints
- The number of nodes in root is in the range [1, 2000].
- The number of nodes in subRoot is in the range [1, 1000].
- -104 <= Node.val <= 104
How to think about it
Updated 2026-09-29A subtree match requires full identity down to leaf boundaries, not merely an embedded prefix path. You are checking whether subRoot equals the tree starting at the current node, or is a subtree of its left child, or a subtree of its right child.
Approaches, worst first
Recursive search with tree comparison
time O(n * m) · space O(h)
For each node in `root`, check if `isSameTree(node, subRoot)` is true. If not, recurse on `node.left` and `node.right`. Simple to implement, but in the worst case of repeated duplicate structures, it compares subtrees redundantly.
Serialization with substring searchWrite this one
time O(n + m) · space O(n + m)
Serialize both trees into unique string representations containing explicit null markers and boundary delimiters (such as `#` and `,`). Use KMP or Robin-Karp to verify whether `subRoot` serialization is a substring of `root` serialization.
Where people lose marks · 3
- Allowing a match where `subRoot` ends but `root` continues down; the problem requires the candidate node to match all descendants, not just a subset.
- In string serialization, forgetting unique node delimiters (like commas): node value 12 could falsely match node value 2 without separators.
- Short-circuiting the recursion when a candidate node fails `isSameTree` instead of continuing to search deeper for alternative matches.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .