11. Symmetric Tree
easyAsked at UnityDecide if a tree is a mirror of itself. Unity uses this to test mirrored-rig validation in animation pipelines.
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. Inorder string compare
Serialize left and right subtrees and reverse-compare.
- Time
- O(n)
- Space
- O(n)
// build inorder strings for left & reversed-right, compare
// works but allocates; not idealTradeoff:
2. Paired DFS
Recurse on (left.left, right.right) and (left.right, right.left) in lockstep with value check.
- 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:
Unity-specific tips
Unity wants paired recursion because mirrored rigs in animation must validate left/right bones in lockstep, not by string compare.
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 Unity interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →