Subtree of Another Tree

EasyTrees · DFS · Tree Matching🧩 Tree DFSLeetCode #572 ↗Updated

Problem

Given two binary trees root and subRoot, return true if some node of root is the top of a subtree that is identical to subRoot — same shape and same values, all the way down to the leaves. A tree also counts as a subtree of itself.
Inputroot = [3,4,5,1,2], subRoot = [4,1,2]Outputtrue
Inputroot = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]Outputfalse
Inputroot = [1,1], subRoot = [1]Outputtrue

1 ≤ nodes in root ≤ 2000  ·  1 ≤ nodes in subRoot ≤ 1000  ·  -104 ≤ Node.val ≤ 104

Try it yourself

Java
class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
A match must be a whole subtree, so it must have the same height as subRoot. Nodes of equal height can never be ancestors of each other, so their subtrees are disjoint — comparing only at those nodes touches every node at most once.
PatternTree DFS

Instinct If the answer for a node can be built from the answers of its left and right subtrees, write one recursive function and trust it on the children.

Memory hack Ask the kids, then decide - each child reports up, the parent combines, and a global variable keeps the best "bend" seen so far.

Open the Tree DFS guide →

Approaches

Brute ForceTry Every Node as a Start

Time O(n · m)Space O(h)

Visit each node of root and run a full same-tree comparison between that node and subRoot. If any comparison succeeds we are done. A comparison can cost up to m steps and there are n starting points.

Java
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
    if (root == null) return false;
    if (same(root, subRoot)) return true;
    return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
private boolean same(TreeNode a, TreeNode b) {
    if (a == null || b == null) return a == b;
    return a.val == b.val && same(a.left, b.left) && same(a.right, b.right);
}

The natural first answer and fine for these limits, but quadratic in the worst case.

At a glance

LevelApproachTimeSpace
Brute ForceTry Every Node as a StartO(n · m)O(h)
BetterSerialize + KMP String SearchO(n + m)O(n + m)
OptimalHeight-Filtered ComparisonO(n + m)O(h)
Common pitfalls
  • Accepting a node whose subtree starts like subRoot but has extra descendants — leaves must line up too.
  • Serializing without null markers or separators — gives false matches like 2 inside 12.
  • Returning after the first node with a matching value; another node with that value may still match.
Interview tips
  • Start with “check every node with Same Tree” and state O(n·m) honestly.
  • Offer serialization + KMP (or rolling hash) as the classic linear upgrade.
  • Close with the height filter — linear time, O(h) space, and easy to prove.
Pattern
Tree matching via DFS + same-tree check → find whether a tree pattern appears as a full subtree; upgrade with serialization or a size/height filter.
My notes saved in this browser