Skip to main content

11. Symmetric Tree

easyAsked at Tesla

Determine whether a binary tree is a mirror of itself.

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

Problem

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

Constraints

  • 1 <= node count <= 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 list compare

Collect inorder traversal, check palindrome.

Time
O(n)
Space
O(n)
const list = [];
(function dfs(n){ if(!n){list.push(null);return;} dfs(n.left); list.push(n.val); dfs(n.right); })(root);
return list.join() === [...list].reverse().join();

Tradeoff:

2. Mirror recursion

Compare left subtree with right subtree pairwise (a.left vs b.right, a.right vs b.left).

Time
O(n)
Space
O(h)
function isSymmetric(root) {
  const 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:

Tesla-specific tips

Tesla autopilot sometimes mirrors LIDAR scans for sanity checks — interviewers like clean recursive structural equality without serializing nulls.

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

Practice these live with InterviewChamp.AI →