Back to problems Solve on LeetCode → ◆ Tree Patterns See #104 Max Depth →

Same Tree

LeetCode 100 • Easy • Trees

Input: p = [1, 2, 3, 4, 5], q = [1, 2, 3, 4, 5]  →  Output: True
Given two binary trees, check if they are structurally identical with the same node values.

TimeO(n)visit each node pair
SpaceO(n)queue of pairs
Matched: 0/5 Queue: 0 Peak Q: 0
Comparing
In Queue
Matched
Link
Queue
empty
Pair
Result
pending
Ready
Press Play to watch BFS compare two trees pair-by-pair, or Step to advance one operation.
Uses a deque of node pairs comparing corresponding positions in both trees.