Trees · Binary Tree Fundamentals

Binary Tree Mirror & Symmetry

Checking whether a tree is a mirror of itself by comparing left/right subtree pairs outward-in, and separately, physically swapping every node's children to produce the mirror image itself - the same "walk both sides together" shape, once as a comparison and once as a mutation.

  • 5 min read
  • 10 practice questions
  • 2 graded problems
On this page
  1. Description
  2. The approach: symmetry check (crossed comparison)
  3. A second flavor: producing a mirror (crossed mutation)
  4. Where you'll see it
  5. Which tool for which job?
  6. Common mistakes
  7. Check yourself
  8. Key takeaways
  9. Quick revise
  10. Practice

Description

A tree is symmetric when its left half is a mirror image of its right half. Mirroring a tree swaps the children of every node. Both rest on one idea: pair nodes crossed, a left child with the other side's right child, instead of in parallel. Checking reads the pairs; mirroring writes them.

We'll go from simple to more technical:

  1. The pattern: a symmetry check comparing crossed pairs.
  2. A second flavor: producing the mirror by swapping at every node.
  3. Where mirrored structure matters in practice.
  4. The parallel-instead-of-crossed bug, and deep trees.

The approach: symmetry check (crossed comparison)

"Mirror" isn't the same as "identical". Hold a mirror to the left half, and its outer edge lines up with the right half's outer edge. So compare the root's two children. Then compare the outer pair: the left one's left child with the right one's right child. Then the inner pair: the left one's right child with the right one's left child. Repeat all the way down.

5

3

3

1

4

4

1

5

3

3

1

4

4

1

Pair comparedValuesNext pairs
the root's children3 and 3: equalthe outer pair and the inner pair
outer: left's left, right's right1 and 1: equalboth missing children, so equal
inner: left's right, right's left4 and 4: equalboth missing children, so equal

The tree is symmetric. Comparing in parallel instead, left's left with right's left, would pair 1 with 4 and wrongly say no.

Java

boolean isSymmetric(TreeNode root) {
    return root == null || isMirror(root.left, root.right);
}
boolean isMirror(TreeNode a, TreeNode b) {
    if (a == null && b == null) return true;
    if (a == null || b == null) return false;
    return a.val == b.val && isMirror(a.left, b.right) && isMirror(a.right, b.left);
}

Python

def is_symmetric(root: TreeNode | None) -> bool:
    return root is None or is_mirror(root.left, root.right)


def is_mirror(a: TreeNode | None, b: TreeNode | None) -> bool:
    if a is None and b is None:
        return True
    if a is None or b is None:
        return False
    return a.val == b.val and is_mirror(a.left, b.right) and is_mirror(a.right, b.left)

A second flavor: producing a mirror (crossed mutation)

To turn a tree into its mirror image, swap the two children of every node. Take a root 5 with children 3 and 8, where 3 has a left child 1. After mirroring, 5's children are 8 and 3, and 1 is now 3's right child.

Java

void mirrorTree(TreeNode root) {
    if (root == null) return;
    TreeNode temp = root.left;
    root.left = root.right;
    root.right = temp;
    mirrorTree(root.left);
    mirrorTree(root.right);
}

Python

def mirror_tree(root: TreeNode | None) -> None:
    if root is None:
        return
    root.left, root.right = root.right, root.left   # tuple assignment swaps without a temp
    mirror_tree(root.left)
    mirror_tree(root.right)

The check reads crossed pairs, and the mirror writes a crossed swap. It's the same idea used two ways.

Where you'll see it

Mirroring comes up when a layout must flip, such as turning a left-to-right UI tree into a right-to-left one for Arabic or Hebrew. It also helps normalize expression trees: for an operation like + where order doesn't matter, swapping operands into a fixed order lets equal expressions compare equal.

Which tool for which job?

You needUse
Is the tree symmetric?compare (left, right) pairs crossed, recursively
The same on a very deep treea queue (or stack) of node pairs
Mirror the tree in placeswap children at every node
A mirrored copy, original untouchedbuild new nodes with crossed children
Are two trees mirrors of each other?the symmetry check on (a, b)

Common mistakes

MistakeWhat happensDo this instead
Comparing a.left with b.leftchecks "same tree", not "mirror"a.left with b.right
Swapping only at the rootonly the top level flipsswap at every node
Checking symmetry via an inorder palindromenon-symmetric trees passcompare the structure, crossed
Mirroring in place when the caller still needs the treethe caller's tree changesbuild a mirrored copy
Recursion on a very deep treestack overflowa queue of pairs

Check yourself

Try to answer before opening each one - recalling beats re-reading.

1. A check compares a.left with b.left and a.right with b.right. What does it really test?

Show answer

Whether the two subtrees are identical copies, not mirror images. That's the "same tree" check. The symmetric tree above fails it, because its halves are mirrored, not copied.

2. A mirror function recurses into the children before swapping them. Is that wrong?

Show answer

No. Each node's swap is independent of its children's swaps, so before or after makes no difference. What matters is that every node gets swapped. A node that's skipped leaves its subtree un-mirrored.

3. Is isSymmetric(root) the same as isSameTree(root.left, root.right)?

Show answer

No. One compares crossed pairs, the other parallel pairs. Most symmetric trees have halves that are mirror images but not identical, so they pass one check and fail the other.

Key takeaways

  • Symmetry compares crossed pairs: a.left with b.right.
  • Comparing in parallel tests for identical halves, a different property.
  • A mirror image swaps the children of every node, not just the root.
  • The check and the mirror are the same crossed pairing, once reading and once writing.

Quick revise

The whole skill on one page, for the night before an interview.

Everything from the lesson and the deep dive on one page. Skim it before practice or an interview, and open the full lesson for anything that doesn't ring a bell.

Pick the tool

You needUse
Symmetric?crossed pairs
Deep treesa queue of pairs
Mirror in placeswap at every node
Mirror, keep originala crossed copy

Snippets worth knowing by heart

Java

boolean mirrorOf(TreeNode a, TreeNode b) {
    if (a == null || b == null) return a == b;
    return a.val == b.val && mirrorOf(a.left, b.right) && mirrorOf(a.right, b.left);   // crossed
}

Python

from collections import deque
def is_symmetric(root) -> bool:
    queue = deque([(root.left, root.right)]) if root else deque()
    while queue:
        a, b = queue.popleft()
        if a is None and b is None:
            continue
        if a is None or b is None or a.val != b.val:
            return False
        queue.append((a.left, b.right))        # crossed
        queue.append((a.right, b.left))
    return True

The traps

  • Parallel comparisons: a.left with b.left checks "same tree", not mirror. Compare a.left with b.right.
  • Swapping only at the top: only the first level flips. Swap at every node.
  • Palindrome shortcuts: an inorder palindrome lets non-symmetric trees pass. Compare the structure, crossed.
  • Mutating a tree the caller needs: their tree changes under them. Build a mirrored copy.
  • Deep recursion: a very deep tree overflows the stack. Use a queue of node pairs.

Staff-level points, one line each

  • Mirroring a BST makes its in-order descending; mirroring twice restores it.
  • Inorder [2, 1, 1, 2] is a palindrome; the tree isn't symmetric.
  • A 200,000-deep V: recursion overflowed, a queue of pairs answered true.
  • Symmetric ⇔ equals its mirror, but the crossed check needs no copy.

Say it in an interview

  • "A mirror pairs nodes crossed: left with right."
  • "I compare values and recurse on (a.left, b.right) and (a.right, b.left)."
  • "To mirror, I swap the children at every node."
  • "For very deep trees I'd use a queue of pairs instead of recursion."

Practice

This skill comes with 10 multiple-choice questions that tell you which misconception a wrong answer reveals and 2 graded problems. Your answers are graded and your mastery is tracked, so you'll know when the skill holds up. Running code and saving progress need a free account.

Practice Binary Tree Mirror & Symmetry free

Mirror Tree

EasyFreeCoding problemJava · Python

Given the root of a binary tree, transform it in place into its mirror image, where every node's left and right children are swapped.

Practice Binary Tree Mirror & Symmetry free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime