Skip to main content

11. Symmetric Tree

easyAsked at Glassdoor

Check if a binary tree is a mirror of itself — Glassdoor uses this to test paired-recursion reasoning.

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

Problem

Given the root of a binary tree, check whether it is a mirror of itself — that is, symmetric around its center. Return true if symmetric and false otherwise.

Constraints

  • Number of nodes is 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 and palindrome check

Inorder traversal then compare to reversed.

Time
O(n)
Space
O(n)
// fragile: ambiguity between null placement and value
const arr = inorder(root);
return arr.join(',') === arr.reverse().join(',');

Tradeoff:

2. Paired DFS

Recurse on (left.left, right.right) and (left.right, right.left); compare values.

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:

Glassdoor-specific tips

Glassdoor expects you to reject the inorder+palindrome trick — they want you to articulate why null-placement matters, which mirrors how their schema-aware review diffs work.

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

Practice these live with InterviewChamp.AI →