11. Symmetric Tree
easyAsked at CanvaCheck whether a binary tree is a mirror of itself — Canva uses this to gauge how clearly you reason about mirrored layer compositions.
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. BFS pairs
BFS treating left and right siblings as pairs.
- Time
- O(n)
- Space
- O(n)
const q = [root.left, root.right];
while (q.length) {
const a = q.shift(), b = q.shift();
if (!a && !b) continue;
if (!a || !b || a.val !== b.val) return false;
q.push(a.left, b.right, a.right, b.left);
}Tradeoff:
2. Recursive mirror
Helper compares one node's left to the other's right and vice versa. Symmetric iff every paired walk matches.
- 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:
Canva-specific tips
Canva expects you to walk through a tiny ASCII tree on the whiteboard, since visual reasoning is half the signal at a design-tools company.
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 Canva interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →