Browse curriculum

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:

  1. Start with (root.left, root.right).
  2. Two nulls are a match; one null or two different values break the mirror.
  3. 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)

2·2

 

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)

Free preview: the first 3 of 9 steps

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 True
Symmetric 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

The approach, step by step

  1. Start with the two subtrees

    Queue the pair (root.left, root.right).

  2. Compare each pair

    Two nulls: skip. One null, or different values: return false.

  3. Queue the children crosswise

    For a matching pair (a, b), queue (a.left, b.right) and (a.right, b.left). If the queue empties, return true.

Frequently asked questions

How do you check if a binary tree is symmetric?

Compare the left and right subtrees as mirrors. Two nodes mirror each other if both are null, or both exist with equal values and their children mirror crosswise: the left child of one with the right child of the other (outer pair), and the right child of one with the left child of the other (inner pair).


Recursion or a queue for Symmetric Tree?

Both work. The recursive version calls isMirror(a.left, b.right) and isMirror(a.right, b.left). The iterative version, shown here, puts the same pairs in a queue, which avoids deep recursion on very skewed trees.


Why is comparing level-order lists not enough?

Checking that each level reads the same forwards and backwards fails without null placeholders: [1, 2, 2, null, 3, null, 3] has levels [1], [2, 2], [3, 3] that look palindromic, yet both 3s sit on the right. Comparing mirror pairs node by node keeps the structure.

New to the technique behind this problem? Watch it run step by step:

Breadth First Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description