Skip to main content

11. Symmetric Tree

easyAsked at Dropbox

Decide whether a binary tree is a mirror of itself; Dropbox uses it to probe pair-pointer recursion patterns that show up in symmetric chunk verification.

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

Problem

Given the root of a binary tree, check whether it is a mirror of itself (the left subtree equals the reflection of the right subtree).

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. Inorder + palindrome

Inorder-traverse with null markers, check if the resulting array is a palindrome.

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

Tradeoff:

2. Mirror DFS

Recurse on (left.left, right.right) and (left.right, right.left). Each step compares matched mirror pairs.

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

Tradeoff:

Dropbox-specific tips

Dropbox interviewers will probe whether you pass the two children or the root — passing children halves the base case and they treat that as a senior signal.

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

Practice these live with InterviewChamp.AI →