Binary Tree
Mirror Image
Symmetric Tree
Tree Algorithms
Data Structures

Check if a binary tree is a mirror image or symmetric

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

A binary tree is symmetric if its left subtree is a mirror reflection of its right subtree. The key idea is not just that the same values appear on both sides, but that they appear in mirrored positions with the same structure.

Define the Mirror Condition Clearly

Two subtrees are mirrors if all of the following hold:

  • both roots are None, or both roots exist,
  • the root values are equal,
  • the left child of one matches the right child of the other,
  • the right child of one matches the left child of the other.

That recursive definition maps directly to code and is why this problem is usually solved with either recursion or a queue.

Recursive Solution

The recursive version is compact and easy to reason about.

python
1class TreeNode:
2    def __init__(self, val=0, left=None, right=None):
3        self.val = val
4        self.left = left
5        self.right = right
6
7
8def is_mirror(a, b):
9    if a is None and b is None:
10        return True
11    if a is None or b is None:
12        return False
13    if a.val != b.val:
14        return False
15
16    return is_mirror(a.left, b.right) and is_mirror(a.right, b.left)
17
18
19def is_symmetric(root):
20    if root is None:
21        return True
22    return is_mirror(root.left, root.right)

This visits each node once, so the time complexity is O(n). The extra space is the recursion stack, which is O(h) where h is the tree height.

Iterative Queue-Based Solution

If you want to avoid recursion, use a queue of node pairs that should mirror each other.

python
1from collections import deque
2
3
4def is_symmetric_iterative(root):
5    if root is None:
6        return True
7
8    queue = deque([(root.left, root.right)])
9
10    while queue:
11        a, b = queue.popleft()
12
13        if a is None and b is None:
14            continue
15        if a is None or b is None:
16            return False
17        if a.val != b.val:
18            return False
19
20        queue.append((a.left, b.right))
21        queue.append((a.right, b.left))
22
23    return True

This version has the same O(n) time complexity. In the worst case, the queue can hold O(n) nodes.

What Symmetry Is Not

Many incorrect solutions compare inorder traversals or simply check whether the left side contains the same multiset of values as the right side. That is not enough. Symmetry is about structure and mirrored position, not just matching values.

For example, these two shapes are not equivalent just because they contain the same numbers:

  • left child on one side paired with left child on the other side,
  • missing node on one side paired with present node on the other side.

Any valid algorithm must compare mirrored positions directly.

Small Example

This tree is symmetric:

text
        1
      /        2     2
    / \   /    3   4 4   3

This tree is not symmetric:

text
        1
      /        2     2
      \            3     3

The values may look close, but the child placement is not mirrored.

Practical Rule

If the problem statement says mirror, compare node pairs in mirrored order. Do not flatten the tree first unless the flattening preserves null positions exactly, which usually makes the solution more complicated than necessary.

Common Pitfalls

  • Comparing only values and ignoring structure.
  • Traversing both sides in the same order instead of mirrored order.
  • Forgetting the case where one node is None and the other is not.
  • Assuming a tree with repeated values is symmetric just because the counts match.
  • Overcomplicating the problem with full serialization when pairwise comparison is enough.

Summary

  • A symmetric tree has left and right subtrees that mirror each other.
  • The standard recursive solution compares mirrored child pairs directly.
  • An iterative queue solution works just as well if recursion depth is a concern.
  • Time complexity is O(n) because every node is checked once.
  • Structure matters as much as node values when testing symmetry.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms