Skip to main content

11. Symmetric Tree

easyAsked at Etsy

Check if a tree mirrors itself around the center — Etsy uses it to probe mirror-recursion fluency.

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

Problem

Given the root of a binary tree, return true if it is a mirror of itself — i.e. symmetric around its center.

Constraints

  • Node count in [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 fingerprint

Inorder serialize and compare to reverse — flaky for null handling.

Time
O(n)
Space
O(n)
function ser(n){ return n ? `(${ser(n.left)},${n.val},${ser(n.right)})` : 'N'; }
const s = ser(root);
return s === s.split('').reverse().join('');

Tradeoff:

2. Pair recursion

Walk left subtree against right subtree, checking outer/inner pairings.

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

Tradeoff:

Etsy-specific tips

Etsy interviewers want a confident recursive decomposition — bonus if you can connect it to A/B-testing symmetric layout trees for listing grids.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →