Skip to main content

14. Symmetric Tree

easyAsked at Square

Determine whether a tree is a mirror image of itself. Square uses this as a sibling problem to Same Tree to see if you can adapt the recursion shape — the kind of pattern transfer they want when porting logic between desktop and mobile POS clients.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Square loops.

  • LeetCode Discuss (2025)Square Capital phone screen.
  • Glassdoor (2026)Cash App backend.

Problem

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

Constraints

  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100

Examples

Example 1

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

Example 2

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

Approaches

1. Recursion with paired pointers

Define mirror(a, b): both null = true; one null = false; a.val == b.val and mirror(a.left, b.right) and mirror(a.right, b.left).

Time
O(n)
Space
O(h)
function isSymmetric(root) {
  const mirror = (a, b) => {
    if (!a && !b) return true;
    if (!a || !b) return false;
    return a.val === b.val && mirror(a.left, b.right) && mirror(a.right, b.left);
  };
  return !root || mirror(root.left, root.right);
}

Tradeoff: Clean post-order recursion. Stack depth = tree height.

2. Iterative BFS with paired queue

Enqueue (left, right) pairs. For each, check equality and enqueue (a.left, b.right) and (a.right, b.left).

Time
O(n)
Space
O(w)
function isSymmetric(root) {
  if (!root) return true;
  const q = [[root.left, root.right]];
  while (q.length) {
    const [a, b] = q.shift();
    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;
}

Tradeoff: Iterative — no stack overflow risk. Square interviewers often follow up with 'now do it iteratively.'

Square-specific tips

Square interviewers grade this on whether you notice the mirror invariant: a.left pairs with b.right, not b.left. Verbalize this before writing code — it shows you grasp the symmetry. Bonus signal: mention that this is Same Tree with one tree flipped.

Common mistakes

  • Pairing a.left with b.left (that's Same Tree, not Symmetric Tree).
  • Forgetting that a single root is trivially symmetric.
  • Returning early on the first null without checking the other side.

Follow-up questions

An interviewer at Square may pivot to one of these next:

  • Iterative version — interviewers ask this 50% of the time.
  • Symmetric N-ary tree — must reverse the entire child list.
  • Check if two trees are reflections of each other (not just self-reflection).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why pair a.left with b.right?

Because we're checking mirror equality — the left subtree of one half should mirror the right subtree of the other.

Is the iterative version preferred in production?

Often yes — it avoids stack-depth limits on adversarial inputs. Square cares about that for trust-boundary code.

Practice these live with InterviewChamp.AI

Drill Symmetric Tree and other Square interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →