Home
DSA Patterns
Breadth-First Search
Symmetric Tree
Symmetric Tree (LeetCode 101)
Symmetric Tree (LeetCode 101): check whether a binary tree is a mirror of itself by comparing mirror pairs through a queue: outer children with outer, inner with inner.
Problem Understanding
Symmetric Tree: given the root of a binary tree, check whether it is a mirror of itself, symmetric around its centre.
Example: [1, 2, 2, 3, 4, 4, 3] is symmetric; [1, 2, 2, null, 3, null, 3] is not, because both 3s hang on the right.
The Intuition: Compare Crosswise
Fold the tree down the middle. Every node on the left must land on a node with the same value on the right. So compare pairs:
- Start with
(root.left, root.right). - Two nulls are a match; one null or two different values break the mirror.
- A matching pair
(a, b)passes the check down crosswise: the outer pair(a.left, b.right)and the inner pair(a.right, b.left).
Processing the pairs with a queue is the same breadth-first search loop as Binary Tree Level Order Traversal, but each queue entry holds two nodes instead of one.
Symmetric Tree Visualization
EACH PAIR = A NODE AND ITS MIRROR IMAGE
QUEUE (LEFT, RIGHT)
pair
:
Answer
:
queue of pairs (front first)
Compare the root's two subtrees as mirrors: queue the pair (2, 2)
Next
(2, 2) match. Queue outer (3, 3) and inner (4, 4)
Tree
The two nodes being compared light up together, one on each side, and matched nodes turn green. The queue of pairs is listed beside the tree. Try Example 2, where the levels look symmetric but the structure is not.
Symmetric Tree Dry Run
Input: root = [1, 2, 2, 3, 4, 4, 3]
| Pair | Result | Queued |
|---|---|---|
| (2, 2) | match | (3, 3), (4, 4) |
| (3, 3) | match | (null, null) × 2 |
| (4, 4) | match | (null, null) × 2 |
| (null, null) × 4 | skip | - |
The queue empties. Answer: true.
Symmetric Tree LeetCode Solution
A queue of node pairs. LeetCode guarantees at least one node, so root is never null.
Symmetric Tree Code
1var isSymmetric = function(root) {2 const queue = [[root.left, root.right]];3 let head = 0;4 while (head < queue.length) {5 const [a, b] = queue[head++];6 if (!a && !b) continue;7 if (!a || !b || a.val !== b.val) return false;8 queue.push([a.left, b.right]);9 queue.push([a.right, b.left]);10 }11 return true;12};
Symmetric Tree in Python, Java, C++, TypeScript and C
Symmetric Tree in Python
from collections import deque
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
queue = deque([(root.left, root.right)])
while queue:
a, b = queue.popleft()
if not a and not b:
continue
if not a or not b or a.val != b.val:
return False
queue.append((a.left, b.right))
queue.append((a.right, b.left))
return TrueSymmetric Tree in Java
class Solution {
public boolean isSymmetric(TreeNode root) {
Deque<TreeNode[]> queue = new ArrayDeque<>();
queue.offer(new TreeNode[]{root.left, root.right});
while (!queue.isEmpty()) {
TreeNode[] pair = queue.poll();
TreeNode a = pair[0], b = pair[1];
if (a == null && b == null) continue;
if (a == null || b == null || a.val != b.val) return false;
queue.offer(new TreeNode[]{a.left, b.right});
queue.offer(new TreeNode[]{a.right, b.left});
}
return true;
}
}Symmetric Tree in C++
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<pair<TreeNode*, TreeNode*>> q;
q.push({root->left, root->right});
while (!q.empty()) {
auto [a, b] = q.front();
q.pop();
if (!a && !b) continue;
if (!a || !b || a->val != b->val) return false;
q.push({a->left, b->right});
q.push({a->right, b->left});
}
return true;
}
};Symmetric Tree in TypeScript
function isSymmetric(root: TreeNode | null): boolean {
const queue: [TreeNode | null, TreeNode | null][] = [[root!.left, root!.right]];
let head = 0;
while (head < queue.length) {
const [a, b] = queue[head++];
if (!a && !b) continue;
if (!a || !b || a.val !== b.val) return false;
queue.push([a.left, b.right]);
queue.push([a.right, b.left]);
}
return true;
}Symmetric Tree in C
bool isSymmetric(struct TreeNode* root) {
struct TreeNode* queue[2002];
int head = 0, tail = 0;
queue[tail++] = root->left;
queue[tail++] = root->right;
while (head < tail) {
struct TreeNode* a = queue[head++];
struct TreeNode* b = queue[head++];
if (!a && !b) continue;
if (!a || !b || a->val != b->val) return false;
queue[tail++] = a->left;
queue[tail++] = b->right;
queue[tail++] = a->right;
queue[tail++] = b->left;
}
return true;
}Edge Cases & Common Mistakes
- One node: both children are null, so the first pair is skipped and the answer is true.
- Comparing the same side: pairing
(a.left, b.left)checks for identical subtrees, not mirrors; always pair crosswise. - Ignoring nulls:
[1, 2, 2, null, 3, null, 3]only fails because a null meets a 3; nulls are part of the shape. - Checking values only per level: a palindromic level is necessary but not sufficient without null placeholders.
- Recursion depth: a deep skewed tree can hit recursion limits; the queue version avoids that.
Summary
Queue the pair (root.left, root.right). For each pair, two nulls match, one null or different values mean false, and a matching pair queues its outer and inner children crosswise. If the queue empties, the tree is symmetric. O(n) time, O(w) space.
More practice: DSA interview questions with answers · patterns cheat sheet
