11. Symmetric Tree
easyAsked at ServiceNowGiven the root of a binary tree, check whether it is a mirror of itself. ServiceNow asks this to confirm you can write a paired recursion (compare left.left with right.right, left.right with right.left) — useful in any mirrored workflow validation.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in ServiceNow loops.
- Glassdoor (2026-Q1)— ServiceNow loops use this to test paired-pointer recursion.
- LeetCode Discuss (2025-12)— ServiceNow phone screen rotation includes this with explicit BFS follow-up.
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
root = [1,2,2,3,4,4,3]trueExample 2
root = [1,2,2,null,3,null,3]falseApproaches
1. Compare with reversed clone
Build a mirror clone, then compare equality (LC 100).
- Time
- O(n)
- Space
- O(n)
function isSymmetric(root) {
const mirror = (n) => !n ? null : { val: n.val, left: mirror(n.right), right: mirror(n.left) };
const same = (a,b) => (!a && !b) || (a && b && a.val === b.val && same(a.left,b.left) && same(a.right,b.right));
return same(root, mirror(root));
}Tradeoff: Allocates a whole second tree. Mention but show why ServiceNow prefers the paired-pointer recursion.
2. Paired recursion
Define isMirror(a, b). Base cases handle null. Recurse: a.left vs b.right AND a.right vs b.left.
- Time
- O(n)
- Space
- O(h)
function isSymmetric(root) {
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, root);
}Tradeoff: Linear time, no extra tree. The trick is mirroring the recursion: left.left pairs with right.right, left.right pairs with right.left.
ServiceNow-specific tips
ServiceNow grades for the paired recursion mirror pattern — they want isMirror(a.left, b.right) AND isMirror(a.right, b.left). Bonus signal: state the invariant 'two subtrees are mirrors iff their roots are equal and their left/right children mirror diagonally' before coding.
Common mistakes
- Pairing left.left with right.left instead of right.right — symmetric error that flips the meaning.
- Returning true when one is null but not the other.
- Comparing whole subtrees as equal — that's same-tree, not mirror-tree.
Follow-up questions
An interviewer at ServiceNow may pivot to one of these next:
- Iterative BFS version using a queue of pairs.
- Tree Diameter (LC 543).
- Detecting symmetric subgraphs in an arbitrary graph.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why call isMirror(root, root) and not isMirror(root.left, root.right)?
Calling on (root, root) lets the base case handle null root uniformly. Calling on the children would need a separate null check on root.
Can I do it iteratively?
Yes — use a queue. Push pairs (a, b). Each step pop a pair, compare, then push (a.left, b.right) and (a.right, b.left).
Practice these live with InterviewChamp.AI
Drill Symmetric Tree and other ServiceNow interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →