11. Symmetric Tree
easyAsked at SpotifyDetermine if a binary tree is a mirror image of itself around the center.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, check whether the tree is a mirror of itself around its center.
Constraints
1 <= nodes <= 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. Inorder serialize
Build serialized left and reverse of right, compare
- Time
- O(n)
- Space
- O(n)
function ser(t){if(!t)return '#';return ser(t.left)+','+t.val+','+ser(t.right);}
// compare ser(root.left) vs reversed ser(root.right) — fragileTradeoff:
2. Paired recursion
Recurse on left.left vs right.right and left.right vs right.left. Cleanly enforces mirror invariant.
- 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:
Spotify-specific tips
Spotify rarely hits symmetric trees in prod, so the bonus signal here is choosing the cleanest recursion invariant and explaining why your base cases are exhaustive.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Symmetric Tree and other Spotify interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →