Skip to main content

12. Symmetric Tree

easyAsked at Datadog

Check whether a binary tree is a mirror of itself (symmetric around its center). Datadog likes this as a paired-recursion warmup — same shape as comparing the inbound vs outbound side of a bidirectional metric pipeline.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Datadog phone screen tree question.
  • LeetCode Discuss (2025-11)Datadog onsite report.

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

Inorder-traverse; check whether the result is a palindrome.

Time
O(n)
Space
O(n)
function isSymmetric(root) {
  const vals = [];
  function dfs(n) {
    if (!n) { vals.push(null); return; }
    dfs(n.left); vals.push(n.val); dfs(n.right);
  }
  dfs(root);
  for (let i = 0, j = vals.length - 1; i < j; i++, j--) {
    if (vals[i] !== vals[j]) return false;
  }
  return true;
}

Tradeoff: Allocates O(n) buffer. Misses structural symmetry — two structurally different trees can produce the same inorder sequence.

2. Mirror recursion (optimal)

Recurse with TWO pointers walking the tree in opposite directions. left.left mirrors right.right; left.right mirrors right.left.

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

Tradeoff: Single pass with two pointers. Datadog interviewers love the paired-recursion pattern; it's the same trick they use to compare upstream/downstream message streams.

Datadog-specific tips

Datadog wants the structural mirror approach, not the serialize-and-compare one. The interviewer is checking whether you can recurse with TWO pointers simultaneously — a pattern that recurs in their pipeline-mirror diff tooling.

Common mistakes

  • Calling isSymmetric(root.left) && isSymmetric(root.right) — that checks if each subtree is symmetric, not if they mirror each other.
  • Forgetting the both-null base case → crash.
  • Comparing a.left with b.left (instead of b.right) — that's same-tree, not mirror.

Follow-up questions

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

  • Determine if Two Trees are Mirrors (general) — two-tree variant.
  • Invert a Binary Tree (LC 226) — modify in place.
  • Flip Equivalent Binary Trees (LC 951).

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 recursion take two pointers?

Symmetry is a relation between two subtrees, not a property of one. Single-pointer recursion can't compare nodes in different subtrees at the same level.

Can this be done iteratively?

Yes — use a queue, enqueue (left, right) pairs, pop and compare them, then enqueue the cross-children pairs (a.left, b.right) and (a.right, b.left).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →