Browse curriculum

Symmetric Tree

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.

Interactive Walkthrough

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.

Dry Run Table

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.

The Solution Template

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};

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.

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

AI resume checker — match your resume to the job description