11. Symmetric Tree
easyAsked at RobloxCheck whether a binary tree is a mirror image of itself.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, determine if the tree is symmetric around its center. The left subtree must be a mirror of the right subtree in both structure and values.
Constraints
1 <= number of 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 string check
Serialize then compare with its reverse.
- Time
- O(n)
- Space
- O(n)
const arr = [];
const dfs = n => { if (!n) { arr.push('#'); return; } arr.push(n.val); dfs(n.left); dfs(n.right); };
dfs(root);
return arr.join(',') === [...arr].reverse().join(',');Tradeoff:
2. Mirror recursion
Pair left and right; recurse with mirrored children.
- Time
- O(n)
- Space
- O(h)
function isSymmetric(root) {
const mirror = (a, b) => {
if (!a && !b) return true;
if (!a || !b || a.val !== b.val) return false;
return mirror(a.left, b.right) && mirror(a.right, b.left);
};
return !root || mirror(root.left, root.right);
}Tradeoff:
Roblox-specific tips
Roblox often uses this to gauge spatial-symmetry intuition before testing whether you can mirror an avatar rig's left/right limbs.
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 Roblox interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →