Skip to main content

12. Symmetric Tree

easyAsked at Wix

Check if a binary tree mirrors itself around its center; Wix uses the helper when validating symmetric layout templates.

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 <= 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. Mirror serialize

Serialize left and mirrored right; compare.

Time
O(n)
Space
O(n)
function ser(n,rev){if(!n) return 'N'; return rev? `(${n.val},${ser(n.right,rev)},${ser(n.left,rev)})` : `(${n.val},${ser(n.left,rev)},${ser(n.right,rev)})`}

Tradeoff:

2. Two-pointer recursion

Compare left's left to right's right, and vice versa.

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

Tradeoff:

Wix-specific tips

Wix gives partial credit for the iterative-queue version; they value when you mention how symmetric checks help auto-detect mirrored hero sections in their templates.

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

Practice these live with InterviewChamp.AI →