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.

Symmetry is not a statement about the tree as a whole, it is a statement about its two halves: the left subtree must be the mirror image of the right subtree. So the question becomes a comparison between two trees, and we already have the machinery for that from Same Tree.

The only thing that changes is what corresponds to what.

A mirror crosses the sides

Hold a tree up to a mirror. The leftmost node of the real tree appears at the far right of the reflection. So when comparing a node a from the left subtree with its counterpart b from the right subtree, their children do not pair up left-with-left. They pair up crossed:

- a.left faces b.right — these are the outer pair, the two positions furthest from the centre line.
- a.right faces b.left — the inner pair, the two closest to the centre.

That is the entire modification. The Same Tree recursion compared (p.left, q.left) and (p.right, q.right); here the calls become (a.left, b.right) and (a.right, b.left). Everything else — both-null true, one-null false, values-differ false — is unchanged, because "reflects" and "equals" have exactly the same failure modes at a single position.

python
def isSymmetric(root):
    if root is None:
        return True
    return mirrors(root.left, root.right)      # the two halves, compared against each other

def mirrors(a, b):
    if a is None and b is None:
        return True                            # both sides stop here — still symmetric
    if a is None or b is None:
        return False                           # one stops, one does not
    if a.val != b.val:
        return False
    return mirrors(a.left, b.right) and mirrors(a.right, b.left)   # crossed
Crucial Notethe recursion runs on two nodes, never one. A common wrong start is to write a single-node recursion asking "is this subtree symmetric?" — but that question cannot be answered locally, because symmetry is a relationship between two different subtrees, not a property of one. The root itself is never compared with anything: it is its own reflection, and the real work begins with its two children.

The second trap is checking values level by level and asking whether each level reads as a palindrome. The third example kills that idea — the level holds 2, 2 on both sides and reads identically forwards and backwards, yet the two nodes hang on the wrong sides of their parents. Symmetry is about matched positions, and only the crossed pairing tracks positions correctly.

Worked example:root 1, children 2 and 2; the left 2 has children 3 and 4; the right 2 has children 4 and 3
- Compare the two 2s. Both exist, values equal. Now recurse in crossed pairs.
- Outer pair: left-2's left child (3) against right-2's right child (3). Equal, and all four of their children are null, so both deeper calls hit the both-null case and return true.
- Inner pair: left-2's right child (4) against right-2's left child (4). Equal, same story below.
- Both calls true, so the 2s mirror, and the tree is symmetric.

Now the failing case, root 1 with children 2 and 2, where each 2 has only a right child holding 3. Compare the 2s: values match. Outer pair: left-2's left is null, right-2's right is 3 — exactly one is null, so false. Correct: for a mirror, the left 2's child would have to appear on the opposite side from the right 2's child, and both are on the right.

Cost, and the family

Each node is examined at most once as part of exactly one pair, so the time is O(N) and the space is O(H) for the recursion. An iterative version pushes node pairs onto a stack, or walks level by level with a queue enqueuing children in crossed order — useful if the tree may be deep.

The point worth extracting is how small the edit was. Same Tree and Symmetric Tree are the same algorithm with a different pairing rule, and that suggests a habit: when a new problem compares two structures, ask what the correspondence is, and plug it into the paired-recursion skeleton rather than starting over. Same-position gives equality; crossed gives mirroring; comparing a tree against every subtree of another gives subtree-containment. The skeleton — both-empty, one-empty, local check, paired recursive calls — is fixed. Only the pairing changes.

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