Skip to content

Symmetric Tree

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:

Symmetric tree 1, 2, 2, 3, 4, 4, 3

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

Example 2:

Asymmetric tree 1, 2, 2, null, 3, null, 3

Input: root = [1,2,2,null,3,null,3]
Output: false

Constraints:

  • The number of nodes in the tree is in the range [1,1000][1, 1000].
  • −100≤Node.val≤100-100 \le Node.val \le 100

Follow-up: Could you solve it both recursively and iteratively?

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:

  1. both are empty, or
  2. both are non-empty, a->val == b->val, and a->left mirrors b->right (the outer pair) and a->right mirrors b->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.

Symmetric tree 1, 2, 2, 3, 4, 4, 3 with mirror pairs coloured alike: the two 2s, the outer 3s and the inner 4s

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

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.

  1. isMirror(a, b): if both are null return true; if exactly one is null return false.
  2. 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.
  3. The answer is isMirror(root->left, root->right).

Complexity.

  • Time: O(n)O(n) — every node is visited once, as part of exactly one pair.
  • Space: O(h)O(h) for the recursion, where h is the tree height — O(log⁡n)O(\log n) when balanced, O(n)O(n) for a degenerate tree.

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.

  1. Push the pair (root->left, root->right) into pending.
  2. While pending is not empty, pop (a, b):
    • if both are null, continue;
    • if exactly one is null or a->val != b->val, return false;
    • push (a->left, b->right) and (a->right, b->left).
  3. Return true.

Complexity.

  • Time: O(n)O(n) — each node enters the queue once.
  • Space: O(w)O(w), where w is the maximum width of the tree — O(n)O(n) in the worst case.
  • Single node: both children are null, so the first pair is (null, null) and the answer is true.
  • 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.

Approach 1: Recursion
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);
}
};
Approach 1: Recursion
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);
}
}
Approach 1: Recursion
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)
Approach 2: Iterative with a queue
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;
}
};
Approach 2: Iterative with a queue
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;
}
}
Approach 2: Iterative with a queue
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 True

Give it a try first

Attempt the problem on LeetCode — the code will still be here.