12. Symmetric Tree
easyAsked at WixCheck if a binary tree mirrors itself around its center; Wix uses the helper when validating symmetric layout templates.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, check whether it is a mirror of itself (symmetric 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. Mirror serialize
Serialize left and mirrored right; compare.
- Time
- O(n)
- Space
- O(n)
function ser(n,rev){if(!n) return 'N'; return rev? `(${n.val},${ser(n.right,rev)},${ser(n.left,rev)})` : `(${n.val},${ser(n.left,rev)},${ser(n.right,rev)})`}Tradeoff:
2. Two-pointer recursion
Compare left's left to right's right, and vice versa.
- Time
- O(n)
- Space
- O(h)
function isSymmetric(root){
const same=(a,b)=>{
if(!a&&!b) return true;
if(!a||!b||a.val!==b.val) return false;
return same(a.left,b.right)&&same(a.right,b.left);
};
return !root||same(root.left,root.right);
}Tradeoff:
Wix-specific tips
Wix gives partial credit for the iterative-queue version; they value when you mention how symmetric checks help auto-detect mirrored hero sections in their templates.
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 Wix interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →