LeetCode ยท #572

Subtree of Another Tree Solution

Given two trees root and subRoot, return true if subRoot is a subtree of root.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Easy

๐Ÿ”—

LeetCode

Problem #572

๐Ÿ—๏ธ

Pattern

Recursion โ€” isSameTree at every node

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, and false otherwise.

Example
Input: root = [3,4,5,1,2], subRoot = [4,1,2] Output: true // The subtree rooted at node 4 matches subRoot exactly
Constraints
โ€ข 1 โ‰ค nodes in root โ‰ค 2000 โ€ข 1 โ‰ค nodes in subRoot โ‰ค 1000 โ€ข -10โด โ‰ค Node.val โ‰ค 10โด
02
Section Two ยท Approach 1

Serialize & String Match โ€” O(m + n)

Serialize both trees with null markers, then check if subRoot's string is a substring of root's string. O(m+n) with KMP but tricky โ€” need careful null markers to avoid false matches like "12" matching "2".

03
Section Three ยท Approach 2

Recursive โ€” isSameTree at Every Node โ€” O(m ยท n)

At each node in root, check if the subtree rooted there is identical to subRoot (using LC 100 Same Tree). If any match โ†’ true. If no node matches โ†’ false.

๐Ÿ’ก Mental model: You have a photo (subRoot) and a building (root). Walk through every room in the building and try to match the photo. If any room's layout matches the photo exactly โ†’ found the subtree.
Algorithm
  • Base: If root is null โ†’ false. If subRoot is null โ†’ true (null is subtree of anything).
  • Check: If isSameTree(root, subRoot) โ†’ true.
  • Recurse: isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot).
04
Section Four ยท Trace

Visual Walkthrough

Check isSameTree at each node of root
ROOT TREE 3 โœ— 3โ‰ 4 4 โœ“ match! 5 1 2 SUBROOT 4 1 2 SEARCH TRACE โ‘  Node 3: isSameTree(3, 4)? root.val=3 โ‰  subRoot.val=4 โ†’ โœ— no match โ‘ก Node 4: isSameTree(4, 4)? Values match โ†’ check children โ†’ (4.left=1 == 4.left=1 โœ“) AND (4.right=2 == 4.right=2 โœ“) โ†’ FOUND! Node 5 never checked โ€” short-circuit on first match. Answer: true โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python

Java โ€” Recursive
class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { if (root == null) return false; if (isSameTree(root, subRoot)) return true; return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); } private boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null || p.val != q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } }
Python โ€” Recursive
class Solution: def isSubtree(self, root, subRoot) -> bool: if not root: return False if self.isSameTree(root, subRoot): return True return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot) def isSameTree(self, p, q) -> bool: if not p and not q: return True if not p or not q or p.val != q.val: return False return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpace
RecursiveO(m ยท n)O(h)
Serialization + KMPO(m + n)O(m + n)
m = nodes in root, n = nodes in subRoot.:
  • In the worst case, we check isSameTree (O(n)) at every node of root (O(m)) โ†’ O(mยทn).
  • For most interview problems, this is acceptable.
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Same treeroot == subRootisSameTree at root โ†’ true immediately.
Leaf subtreesubRoot = [2]Single-node match โ€” must be a leaf in root (no extra children).
Partial matchroot=[1,2,3], sub=[1,2]Root has extra child (3) that subRoot doesn't โ†’ false at root. But check deeper nodes.
โš  Common Mistake: Confusing "subtree" with "substructure." A subtree must match from the given node downward including all leaves. If root has [4,1,2,0] and subRoot is [4,1,2], it's false because node 1 has an extra child 0 in root.

โ† Back to Trees problems