Skip to main content

11. Symmetric Tree

easyAsked at Booking

Detect mirror symmetry in a binary tree — Booking uses this to test structural-recursion fluency on hierarchical region indexes.

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

Problem

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

Constraints

  • 1 <= number of nodes <= 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. Stringified compare

Serialize left and reversed-right subtrees and compare.

Time
O(n)
Space
O(n)
// build inorder of left subtree and reverse-inorder of right and compare
const l=[],r=[];
function inL(n){if(!n)return l.push(null); inL(n.left); l.push(n.val); inL(n.right);}
function inR(n){if(!n)return r.push(null); inR(n.right); r.push(n.val); inR(n.left);}
inL(root.left); inR(root.right);
return l.join() === r.join();

Tradeoff:

2. Mirror recursion

Compare left.left with right.right and left.right with right.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 mirror(root.left, root.right);
}

Tradeoff:

Booking-specific tips

Booking values clean recursive invariants — relate the mirror check to verifying that a destination tree's left/right hemispheres index consistently.

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 Booking interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →