11. Symmetric Tree
easyAsked at SlackCheck whether a binary tree is a mirror of itself.
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
Nodes count in [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. Inorder + check palindrome
Inorder traverse with null markers, check if it's a palindrome.
- Time
- O(n)
- Space
- O(n)
const arr=[];
function dfs(n){ if(!n){arr.push(null);return;} dfs(n.left);arr.push(n.val);dfs(n.right);}
dfs(root);
return arr.join(',')===arr.reverse().join(',');Tradeoff:
2. Mirror recursion
Recurse on (left.left, right.right) and (left.right, right.left). Treat null mirrors as equal.
- Time
- O(n)
- Space
- O(h)
function isSymmetric(root) {
function 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:
Slack-specific tips
Slack interviewers like the mirror-recursion pattern because it generalizes to UI-tree comparison (RTL vs LTR layouts) — call that out.
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 Slack interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →