Symmetric Tree
Given the root of a binary tree, return true if the tree is a mirror image of itself around its centre line — that is, if its left subtree and right subtree are reflections of one another in both shape and values. A tree with only a root is symmetric, and so is an empty tree.
- The number of nodes in the tree is in the range [1, 1000]
- -100 <= Node.val <= 100
- Both shape and values must mirror
- A single node is symmetric
root = [1,2,2,3,4,4,3]trueroot = [1,2,2,null,3,null,3]falseroot = [1,2,2,2,null,2]falseroot = [1]trueA tree is symmetric when it's its own mirror image — fold it straight down the middle and the two halves land exactly on each other.
1
/ \
2 2
/ \ / \
3 4 4 3 fold at the center -> the left half must mirror the right halfSo this isn't a question about one node — it's about the two halves matching. Compare the left subtree against the right subtree… but crossed, because a mirror swaps left and right.
Walk both halves together. Stand on a pair: a from the left half, b from the right half, at mirror positions. Because a mirror flips sides, their children pair up crossed:
a b
/ \ / \
aL aR bL bR
outer pair: aL <-> bR (furthest from the center)
inner pair: aR <-> bL (closest to the center)At each pair, the same four checks any matched walk uses:
- both null → fine, return true
- one null → shapes differ → false
- values differ → false
- both match → recurse the outer pair AND the inner pair.
def isSymmetric(root):
if root is None:
return True
return mirrors(root.left, root.right) # compare the two halves
def mirrors(a, b):
if a is None and b is None:
return True # both stop here — still mirrored
if a is None or b is None:
return False # one stops, one doesn't
if a.val != b.val:
return False
return mirrors(a.left, b.right) and mirrors(a.right, b.left) # crossedSecond trap: checking each level for a palindrome. A tree like root 1 with four 2's below ([1,2,2,2,null,2]) can have a level reading 2, 2 identically forwards and backwards, yet the nodes hang on the wrong sides of their parents. Only the crossed pairing tracks positions, which is what symmetry is really about.
root 1, children 2 and 2; left-2 has children 3, 4; right-2 has children 4, 3:
1
/ \
2 2 compare the two 2's: equal -> go crossed
/ \ / \
3 4 4 3
outer: left-2.left (3) <-> right-2.right (3) equal
inner: left-2.right (4) <-> right-2.left (4) equal
-> both mirror -> SYMMETRICFailing case: each 2 has only a right child holding 3. Outer pair = (left-2.left = null, right-2.right = 3) → one null → false. Correct — a mirror needs those children on opposite sides, but both sit on the right.
Each node is visited once inside exactly one pair: O(N) time, O(H) stack. The takeaway is how little changed to turn "are these equal?" into "are these mirror images?" — just the pairing. When a problem compares two structures, ask what corresponds to what and drop it into the same paired-recursion skeleton (both-empty, one-empty, local check, paired calls): same-position pairing gives equality, crossed pairing gives mirroring.
Symmetric Tree Check
Mirror Geometry Verification
"Mirror Check: Compare Opposite Sides."
Strategy
A tree is symmetric if it is a mirror image. compare opposite subtrees recursively.
"Is symmetry preserved?"