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.
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.
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.
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:
This tree is not symmetric:
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
Noneand 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
- Check if a list is a rotation of another list that works with duplicates
- Check if a permutation of a string can become a palindrome
- Check if a string is rotation of another WITHOUT concatenating
- check if a tree is a binary search tree
- Check if a given key already exists in a dictionary
- Check if a given key already exists in a dictionary and increment it
- Check if array B is a permutation of A
- Check if edge is included in SOME MST in linear time non-distinct values

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