๐ฒ Symmetric Tree (LeetCode 101) โ Recursive and Iterative Solutions Explained

Checking whether a binary tree is symmetric is a classic recursion problem โ and a great way to learn how to traverse a tree in mirrored fashion. In this post, we'll cover both the recursive and iterative solutions to LeetCode 101 with clear explanations, diagrams, and code.
๐ Problem Statement
Given the
root
of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
โ Examples
Input: [1, 2, 2, 3, 4, 4, 3]
Output: true
Input: [1, 2, 2, null, 3, null, 3]
Output: false
๐ง Core Concept
A binary tree is symmetric if the left and right subtrees are mirror images of each other.
That means:
left.left
==right.right
left.right
==right.left
๐ Recursive Solution (Top-Down Mirror Check)
๐ก Idea
We define a helper function compare(left, right)
that checks whether two subtrees are mirror images.
โ Java Code
class Solution {
public boolean isSymmetric(TreeNode root) {
return compare(root.left, root.right);
}
private boolean compare(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left == null || right == null) return false;
if (left.val != right.val) return false;
return compare(left.left, right.right) && compare(left.right, right.left);
}
}
๐ Breakdown
At each node, we check:
If both nodes are
null
: โ๏ธ symmetricIf only one is
null
: โ not symmetricIf values differ: โ not symmetric
Recursively check:
Outer pair:
left.left
vsright.right
Inner pair:
left.right
vsright.left
๐ Iterative Solution Using Deque
๐ก Idea
We simulate the comparison of mirrored nodes using a double-ended queue (Deque
):
Enqueue
left
to the frontEnqueue
right
to the backPop them and compare, then enqueue their child nodes in mirrored order
โ Java Code
class Solution {
public boolean isSymmetric(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.offerFirst(root.left);
deque.offerLast(root.right);
while (!deque.isEmpty()) {
TreeNode left = deque.pollFirst();
TreeNode right = deque.pollLast();
if (left == null && right == null) continue;
if (left == null || right == null || left.val != right.val) return false;
// Mirror order insert
deque.addFirst(left.left);
deque.addFirst(left.right);
deque.addLast(right.right);
deque.addLast(right.left);
}
return true;
}
}
๐ Visual
1
/ \
2 2
/ \ / \
3 4 4 3
Check:
(2, 2)
(3, 3) and (4, 4)
Each level mirrors the other
๐ง Time and Space Complexity
Metric | Recursive | Iterative |
Time | O(n) | O(n) |
Space | O(h) stack (recursion) | O(n) queue |
n
= number of nodesh
= height of tree
โ Summary
Topic | Notes |
Problem Type | Binary Tree, Recursion, BFS, Mirror |
Difficulty | Easy (but very interview-friendly) |
Key Skills | Recursive thinking, pointer comparison |
Interview Bonus | Also tests your ability to write clean, readable logic |
๐ง Takeaways
Symmetric trees = mirrored traversal
Recursive and iterative solutions follow the same mirror pattern logic
Always check
null
before accessing.val
to avoid NPE
โ๏ธ My Practice Notes
Solved using both approaches
Time taken: ~10 mins
Could easily explain this in a real interview
๐ Related Problems
100. Same Tree
226. Invert Binary Tree
110. Balanced Binary Tree
Subscribe to my newsletter
Read articles from CHAO DU directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
