Skip to main content

12. Symmetric Tree

easyAsked at Coinbase

Check whether a binary tree is a mirror of itself. Coinbase uses this to confirm you can recurse on pairs — the same pattern used to compare a bid ladder with its ask mirror.

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

Source citations

Public interview reports confirming this problem appears in Coinbase loops.

  • Glassdoor (2026-Q1)Coinbase tree-recursion warm-up.

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. Inorder traversal palindrome check

Inorder-walk, check the value list is a palindrome.

Time
O(n)
Space
O(n)
function isSymmetric(root) {
  const vals = [];
  function inorder(n) { if (!n) { vals.push(null); return; } inorder(n.left); vals.push(n.val); inorder(n.right); }
  inorder(root);
  for (let i = 0, j = vals.length - 1; i < j; i++, j--) if (vals[i] !== vals[j]) return false;
  return true;
}

Tradeoff: Works but allocates O(n) space and is finicky with null sentinels.

2. Mirror-recurse on pairs

Define isMirror(a, b): both null → true; one null → false; else a.val == b.val AND isMirror(a.left, b.right) AND isMirror(a.right, b.left). Call with (root.left, root.right).

Time
O(n)
Space
O(h) recursion
function isSymmetric(root) {
  function 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: O(n) time, O(h) space. The 'mirror' helper is the clean abstraction — call it out by name.

Coinbase-specific tips

Coinbase grades the helper-function abstraction — naming it 'mirror' shows you understand the recursive predicate. They sometimes follow with 'is the bid book at price p a mirror of the ask book at price -p?' to probe domain transfer.

Common mistakes

  • Calling isSameTree instead of a mirror helper — same shape, wrong recursion.
  • Forgetting the (one-null, other-not) case — only checking both-null returns true wrongly.
  • Iterating instead of recursing without two queues — single queue loses the pairing.

Follow-up questions

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

  • Iterative version with a queue of pairs.
  • Return the depth at which symmetry first breaks.
  • Symmetric tree where leaf labels are tolerant by epsilon.

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 a separate helper instead of calling isSymmetric recursively?

isSymmetric takes one tree; the recursion needs two. The two-argument helper makes the recursion type-correct and reads cleanly.

How does the iterative version look?

Use a queue. Enqueue (root.left, root.right). Dequeue pairs, compare, and enqueue (a.left,b.right) and (a.right,b.left).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →