π² 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
rootof 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.rightleft.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.leftvsright.rightInner pair:
left.rightvsright.left
π Iterative Solution Using Deque
π‘ Idea
We simulate the comparison of mirrored nodes using a double-ended queue (Deque):
Enqueue
leftto the frontEnqueue
rightto 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
nullbefore accessing.valto 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