Same Tree
Same Tree is LeetCode 100 (Easy). You get the roots p and q of two binary trees. Return true if they are the same tree and false otherwise.
- Same means the same shape, with equal values at every matching position.
- Either tree can be empty, and each has at most 100 nodes.
- The number of nodes in both trees is in the range [0, 100].
- -10⁴ <= Node.val <= 10⁴
Intuition
Two identical binary trees agree in three ways: their roots hold the same value, their left subtrees are identical, and their right subtrees are identical. The last two are the same question asked about smaller trees, so the whole check is one recursive function on a pair of nodes in the same position.
- Both missing: there is nothing to differ, so the pair matches.
- One missing: one tree has a node where the other has none, so the shapes differ.
- Both present: the values must be equal, and then both child pairs must match.
The first mismatch anywhere decides the answer, so the walk stops there.
When a question asks whether two trees match position by position, walk them together and compare one pair of nodes per call. The base cases are where shape differences show up.
Approach
Before reading on, list every way a pair of nodes can look (both missing, one missing, both present) and decide the answer for each. Aim for a single walk over both trees.
Settle the missing nodes first
If p and q are both null, return true: two empty subtrees are equal. If exactly one is null, return false. Checking the two-null case first matters, because the one-null test would also fire on two nulls. After both checks, reading .val is safe.
Compare the two values
With both nodes present, compare p.val and q.val. If they differ, return false without looking at either subtree: whatever lies below, the trees already disagree at this position.
Recurse on the left pair, then the right pair
Return the result for (p.left, q.left) and the result for (p.right, q.right). Each call gets two nodes in the same position, so a false from any depth travels straight back up to the root.
Why the first false ends the walk
and skips its right side once the left side is false, so a failing left pair means the right pair is never visited. The slow case is two trees that really are identical: then every pair of nodes gets checked exactly once.
Code in Python, C++ and Java
and to the root. The rest of the nodes are never visited, because and stops at the first false.and to the root. The rest of the nodes are never visited, because and stops at the first false.Common pitfalls
Pairing left with right
return (self.isSameTree(p.left, q.right)
and self.isSameTree(p.right, q.left))return (self.isSameTree(p.left, q.left)
and self.isSameTree(p.right, q.right))Crossing the children tests whether one tree is the mirror of the other, which is Symmetric Tree's check. p = [1,2,3], q = [1,3,2] returns true, and two copies of [1,2,3] return false.
Joining the subtree results with or
return (self.isSameTree(p.left, q.left)
or self.isSameTree(p.right, q.right))return (self.isSameTree(p.left, q.left)
and self.isSameTree(p.right, q.right))With or, one matching side is enough. p = [1,2,3], q = [1,2,4] returns true because the left subtrees match, even though the right children differ.
Edge cases
p = [], q = []The first check sees two nulls and returns true at once.
p = [], q = [0]The second check returns false before any value is read, so there is no null dereference.
Complexity
Same Tree FAQ
Can you compare the two traversals instead?
Only if the traversal also records the empty children. The plain preorder of [1,2] and of [1,null,2] is 1, 2 for both, yet the shapes differ. Writing # for every null gives 1 2 # # # and 1 # 2 # #, which do differ, but the direct pair walk does the same job without building any strings.
Can it be solved without recursion?
Yes. Push the pair (p, q) onto a stack, then pop a pair, run the same three checks, and push its left pair and its right pair. A queue instead of a stack compares the trees level by level. Both check each pair once; they only swap the call stack for your own.
How does Same Tree help with Subtree of Another Tree?
Subtree of Another Tree runs this exact check starting from every node of the bigger tree, and answers true as soon as one starting node matches the smaller tree completely.