Skip to main content

11. Symmetric Tree

easyAsked at Salesforce

Determine if a binary tree is a mirror of itself around its center. Salesforce uses this to test mirrored recursion — the key insight that the recursive subproblem differs from the surface problem.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce platform team uses this as a warmup before more complex tree recursion.
  • LeetCode Discuss (2025)Common pairing with Same Tree on the same interview loop.

Problem

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

Constraints

  • The number of nodes in the tree is in the range [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. Serialize left and right, reverse one

Serialize the left subtree, serialize the right subtree reversed, compare.

Time
O(n)
Space
O(n)
function isSymmetric(root) {
  if (!root) return true;
  const left = JSON.stringify(serializeLR(root.left));
  const right = JSON.stringify(serializeRL(root.right));
  return left === right;
}

Tradeoff: Works but expensive. Salesforce wants the direct mirrored recursion.

2. Mirrored recursion

Define isMirror(a, b): same value, AND a.left mirrors b.right, AND a.right mirrors b.left.

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

Tradeoff: The key insight is that the recursive call takes TWO nodes (not one). The cross-comparison (left.left vs right.right, left.right vs right.left) is what tests for mirror symmetry.

Salesforce-specific tips

Salesforce loves this problem because it forces candidates to articulate that the subproblem (isMirror with two nodes) is different from the surface problem (isSymmetric with one root). Bonus signal: explain that the recursive helper enables the mirror-pairing logic that the single-argument version can't express.

Common mistakes

  • Defining the recursion as isSymmetric(node) instead of isMirror(a, b) — can't express the cross-pairing.
  • Comparing left.left to right.left (same side) — that tests for identical trees, not mirrored.
  • Forgetting the both-null base case.

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Iterative version using a queue with pairs.
  • Invert Binary Tree (LC 226).
  • Check if a tree is symmetric ignoring values (structure only).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the helper take two arguments?

Because mirror symmetry is a relationship BETWEEN two subtrees, not a property of one. The pair-recursion expresses 'these two are mirrors' naturally.

Why isMirror(left.left, right.right)?

Because in a mirror, the leftmost of the left subtree should match the rightmost of the right subtree. That's the cross-pairing that defines mirror symmetry.

Practice these live with InterviewChamp.AI

Drill Symmetric Tree and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →