Problem
Section titled “Problem”Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:

Input: root = [1,2,2,3,4,4,3]Output: trueExample 2:

Input: root = [1,2,2,null,3,null,3]Output: falseConstraints:
- The number of nodes in the tree is in the range .
Follow-up: Could you solve it both recursively and iteratively?
Solution approach
Section titled “Solution approach”A tree is symmetric when its left subtree is the mirror image of its right subtree. Two trees a and b are mirrors of each other exactly when:
- both are empty, or
- both are non-empty,
a->val == b->val, anda->leftmirrorsb->right(the outer pair) anda->rightmirrorsb->left(the inner pair).
Both approaches below check this definition pair by pair; they differ only in whether the pending pairs are kept on the call stack or in an explicit queue.
In Example 1, nodes of the same colour are compared with each other: first the two 2s, then the outer pair (3, 3), then the inner pair (4, 4).
Approach 1: Recursion
Section titled “Approach 1: Recursion”Intuition. The definition above is already recursive: isMirror(a, b) handles the empty cases and the value check, then recurses on the outer and inner pairs.
Algorithm.
isMirror(a, b): if both are null returntrue; if exactly one is null returnfalse.- Return
a->val == b->val && isMirror(a->left, b->right) && isMirror(a->right, b->left). The&&short-circuits, so the search stops at the first mismatch. - The answer is
isMirror(root->left, root->right).
Complexity.
- Time: — every node is visited once, as part of exactly one pair.
- Space: for the recursion, where
his the tree height — when balanced, for a degenerate tree.
Approach 2: Iterative with a queue
Section titled “Approach 2: Iterative with a queue”Intuition. Replace the call stack with a queue of node pairs that must mirror each other. Pop a pair, check it, and push its outer and inner child pairs.
Algorithm.
- Push the pair
(root->left, root->right)intopending. - While
pendingis not empty, pop(a, b):- if both are null, continue;
- if exactly one is null or
a->val != b->val, returnfalse; - push
(a->left, b->right)and(a->right, b->left).
- Return
true.
Complexity.
- Time: — each node enters the queue once.
- Space: , where
wis the maximum width of the tree — in the worst case.
Edge cases
Section titled “Edge cases”- Single node: both children are null, so the first pair is
(null, null)and the answer istrue. - Same values, different shape (Example 2): the inner pair is
(null, 3), which fails the “exactly one is null” check.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { bool isMirror(TreeNode* a, TreeNode* b) { if (!a || !b) return a == b; // both null -> true, exactly one null -> false return a->val == b->val && isMirror(a->left, b->right) && isMirror(a->right, b->left); }
public: bool isSymmetric(TreeNode* root) { return isMirror(root->left, root->right); }};class Solution { private boolean isMirror(TreeNode a, TreeNode b) { if (a == null || b == null) return a == b; // both null -> true, exactly one null -> false return a.val == b.val && isMirror(a.left, b.right) && isMirror(a.right, b.left); }
public boolean isSymmetric(TreeNode root) { return isMirror(root.left, root.right); }}class Solution: def isSymmetric(self, root: TreeNode | None) -> bool: def is_mirror(a, b): if not a or not b: return a is b # both None -> True, exactly one None -> False return a.val == b.val and is_mirror(a.left, b.right) and is_mirror(a.right, b.left)
return is_mirror(root.left, root.right)class Solution { public: bool isSymmetric(TreeNode* root) { queue<pair<TreeNode*, TreeNode*>> pending; pending.push({root->left, root->right}); while (!pending.empty()) { auto [a, b] = pending.front(); pending.pop(); if (!a && !b) continue; if (!a || !b || a->val != b->val) return false; pending.push({a->left, b->right}); pending.push({a->right, b->left}); } return true; }};class Solution { public boolean isSymmetric(TreeNode root) { // ArrayDeque rejects null elements, so each pair travels as a two-element array. ArrayDeque<TreeNode[]> pending = new ArrayDeque<>(); pending.offer(new TreeNode[] {root.left, root.right}); while (!pending.isEmpty()) { TreeNode[] pair = pending.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; pending.offer(new TreeNode[] {a.left, b.right}); pending.offer(new TreeNode[] {a.right, b.left}); } return true; }}from collections import deque
class Solution: def isSymmetric(self, root: TreeNode | None) -> bool: pending = deque([(root.left, root.right)]) while pending: a, b = pending.popleft() if not a and not b: continue if not a or not b or a.val != b.val: return False pending.append((a.left, b.right)) pending.append((a.right, b.left)) return TrueGive it a try first
Attempt the problem on LeetCode — the code will still be here.
