Algorithm

Symmetric Tree

Trees Pattern

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.

CONSTRAINTS
  • 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
EXAMPLE 1
Input: root = [1,2,2,3,4,4,3]
Output: true
The second level reads 2, 2 and the third reads 3, 4, 4, 3 — each level is a palindrome, and the positions correspond correctly across the centre.
EXAMPLE 2
Input: root = [1,2,2,null,3,null,3]
Output: false
Both 2s have a single child holding 3, but both children hang on the *right*. A reflection would require the left 2's child on its right to face the right 2's child on its left.
EXAMPLE 3
Input: root = [1,2,2,2,null,2]
Output: false
Every value is 2 below the root, so any check based on values alone would pass. The shapes are not mirrored — the left 2's child is on its left, the right 2's on its right — and that is enough to fail.
EXAMPLE 4
Input: root = [1]
Output: true
Both subtrees are empty, and two empty trees mirror each other trivially.
Does a single-node tree count as symmetric?
Yes, and so does an empty one. Both reduce to comparing two empty subtrees.
Is it enough for each level to read the same forwards and backwards?
No, and this is a genuine trap. A level can be a palindrome while the nodes sit under the wrong parents — the correspondence has to be positional, not just level-wide.
Does symmetry mean the values are duplicated?
Mirrored positions must hold equal values, so yes, apart from the root every value appears in a matched pair. The converse fails: matching values do not imply mirrored shape.
Can I invert one subtree and compare with Same Tree?
Yes, that is a legitimate solution — mirror the left subtree, then test it against the right. It costs an extra pass and either mutates the tree or copies it, which is why comparing in mirrored order directly is preferred.

A 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.

text
        1
       / \
      2   2
     / \ / \
    3  4 4  3     fold at the center -> the left half must mirror the right half

So 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.

Standing on a mirrored pair

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:

text
       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.

python
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)   # crossed
Crucial Notethe recursion stands on two nodes, never one. A tempting wrong start is a single-node "is this subtree symmetric?" — but symmetry is a relationship between two subtrees, not a property of one, so it can't be answered locally. The root is its own reflection and is never compared; the real work starts at its two children.

Second 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.

Watch it check

root 1, children 2 and 2; left-2 has children 3, 4; right-2 has children 4, 3:

text
        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 -> SYMMETRIC

Failing 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.

The idea to reuse

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.

Interactive Strategy Visualization

Symmetric Tree Check

Mirror Geometry Verification

1223443
AUDITING...
Mirror Audit Pipeline

"Mirror Check: Compare Opposite Sides."

Strategy

A tree is symmetric if it is a mirror image. compare opposite subtrees recursively.

"Is symmetry preserved?"

O(N) Time · O(N) Space Invert Then Compare
O(N) Time · O(H) Space Crossed Paired Walk