Determine whether two binary trees have identical shapes and equal values in every corresponding position.
Problem
Input. Two roots, first and second, of finite binary trees without cycles or shared children.
Output. Return True exactly when all corresponding node values and child positions match.
Rules. Compare structure as well as values; a left child does not match a right child.
Values use Python ==. Nodes need not have the same object identities.
Preserve both trees. Two empty trees match; exactly one empty tree does not.
Example. Root 1 with children 2 and 3 does not match root 1 with only left child 2.
Their compact level-order notations are [1, 2, 3] and [1, 2, null]; return False.
Picture
Place the two roots together, then compare their corresponding child positions.
Roots 1/1 pass; left children 2/2 and their missing children pass.
Right pair 3/None fails, so the complete comparison fails.
Problem trace
Separate equal-value trees: corresponding child positions still differ
Reference drawing
Problem trace
Matching values are insufficient when corresponding child structure differs
Execution trace
Tree A
- 1compare pair
- 2
- None
- None
- 3
- None
- None
- 2
Tree B
- 1compare pair
- 2
- None
- None
- None
- 2
- node A:1 = B:1
- compare pair
- call stack
- [(A:1,B:1)]
- check
- both real; 1 == 1
Tree A
- 1
- 22 == 2
- None
- None
- 3
- None
- None
- 22 == 2
Tree B
- 1
- 22 == 2
- None
- None
- None
- 22 == 2
- node A:2 = B:2
- not shown→2 == 2
- node A:1 = B:1
- compare pair→not shown
- call stack
- [(A:1,B:1)]→[(A:1,B:1),(A:2,B:2)]
Tree A
- 1
- 2null/null -> true
- None
- None
- 3
- None
- None
- 2null/null -> true
Tree B
- 1
- 2null/null -> true
- None
- None
- None
- 2null/null -> true
- node A:2 = B:2
- 2 == 2→null/null -> true
- base cases
- not shown→(None,None) -> true twice
- return to
- not shown→(A:2,B:2)
Tree A
- 1
- 2return true
- None
- None
- 3next pair
- None
- None
- 2return true
Tree B
- 1
- 2return true
- None
- None
- Nonenext pair
- 2return true
- node A:2 = B:2
- null/null -> true→return true
- node A:3 / B:null
- not shown→next pair
- expression
- not shown→true and true and true
Tree A
- 1
- 2
- None
- None
- 3real/null -> false
- None
- None
- 2
Tree B
- 1
- 2
- None
- None
- Nonereal/null -> false
- 2
- node A:3 / B:null
- next pair→real/null -> false
- node A:2 = B:2
- return true→not shown
- branch
- not shown→first is None or second is None
Tree A
- 1return false
- 2
- None
- None
- 3shape differs
- None
- None
- 2
Tree B
- 1return false
- 2
- None
- None
- Noneshape differs
- 2
- node A:1 = B:1
- not shown→return false
- node A:3 / B:null
- real/null -> false→shape differs
- expression
- not shown→true and true and false
- Recognize it
- Use paired DFS when equality, symmetry, or structural correspondence requires comparing both value and shape at the same tree positions rather than comparing traversed value sequences.
- Keep true
- same_tree(first, second) returns true exactly when the two subtrees rooted at that pair have identical shape and equal values at every corresponding node.
- Reuse it
- When comparing recursive structures, recurse on aligned pairs and make missing-object cases explicit before reading values; this transfers to mirror symmetry, subtree equality, and AST comparison.
Each call answers one exact question: do the complete subtrees at these aligned roots match?
Handle missing nodes before reading values or child fields. Both missing succeeds; exactly one missing fails. For real nodes, require value equality and both child comparisons.
Code
def same_tree(first: TreeNode | None, second: TreeNode | None) -> bool:
if first is None or second is None:
return first is second
return (
first.val == second.val
and same_tree(first.left, second.left)
and same_tree(first.right, second.right)
)
The and chain stops at its first failure: values, then left pair, then right pair.
Worst-case time is in total input nodes, with stack space along compared paths.
A mismatch can stop earlier; a deep chain can exceed Python’s recursion limit.
Failure Check
[1,2] and [1,null,2] have the same preorder values but different child positions.
Separate allocations can still match; real-node identity is not required.
Both empty trees match; only one empty tree does not.
This recursion is not cyclic-graph equivalence.
Change the Alignment
New task: Check whether one tree is the mirror of the other.
Picture and change: Cross left/right pairings, while retaining every value and missing-node check.
def mirror_equal(first, second):
if first is None or second is None:
return first is second
return (
first.val == second.val
and mirror_equal(first.left, second.right)
and mirror_equal(first.right, second.left)
)
Check: [1,2,3] and [1,3,2] pass mirror equality but fail ordinary equality.
Neither input is mutated.
Use when: Correspondence is reflected, not position-for-position. This compares the mirror relation; it does not invert a tree.
Change the Compared Fields
New task: Compare shape only.
Picture and change: Remove value equality, but retain aligned child positions and missing-node cases.
def same_shape(first, second):
if first is None or second is None:
return first is second
return same_shape(first.left, second.left) and same_shape(first.right, second.right)
Check: [1,2] and [9,8] pass.
[1,2] and [1,null,2] still fail.
Both variants retain the original worst-case time, stack and recursion-limit qualifications.
Use when: The specification declares value labels irrelevant.
Transfer Check
Why is first is second correct in the original missing-node branch?
Answer
At least one argument is None there.
Identity succeeds only when both are None; it imposes no identity requirement on real nodes.