02
Intuition
Symmetry is Same Tree with a twist: compare the left subtree against the mirrored right subtree. So the recursion pairs a.left with b.right and a.right with b.left.
03
Approach
1
Mirror comparison
mirror(a, b) = a.val == b.val AND mirror(a.left, b.right) AND mirror(a.right, b.left).
2
Start with the root's children
The root always mirrors itself; the question is mirror(root.left, root.right).
3
Iterative variant
A queue of node pairs does the same pairing breadth-first — useful when recursion depth is a worry.
04
Solution & live demo
python
▶1class Solution:
▶2 def isSymmetric(self, root):
▶3 def mirror(a, b):
▶4 if not a and not b: return True
▶5 if not a or not b: return False
▶6 return (a.val == b.val
▶7 and mirror(a.left, b.right)
▶8 and mirror(a.right, b.left))
▶9 return not root or mirror(root.left, root.right)
05
Edge cases
[1,2,2,null,3,null,3]
Shape matches but not mirrored (both 3s on the same side) → False — the cross-pairing catches it.
Empty tree
Symmetric by convention → True.
06
Complexity
Time
O(n)
Space
O(h)
Each node visited once in a pair.