Back to problems Solve on LeetCode → ◆ Tree Patterns See #100 Same Tree →

Subtree of Another Tree

LeetCode 572 • Easy • Trees

Input: root = [3, 4, 5, 1, 2], subRoot = [4, 1, 2]  →  Output: True
Given two binary trees, check if subRoot is a subtree of root (identical structure and values).

TimeO(m·n)each root node vs subRoot
SpaceO(m+n)outer + inner queues
Checked: 0/5 Outer Q: 0 Peak Q: 0
Candidate
Comparing
In Queue
Matched
Outer Q
empty
Check
Result
pending
Ready
Press Play to watch outer BFS traverse the root tree, calling isSameTree at each node to check for a subtree match.