12. Symmetric Tree
easyAsked at CircleCIDetermine 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, return true if it is a mirror of itself (symmetric around its center). Solve it both recursively and iteratively.
Constraints
1 <= number of nodes <= 1000-100 <= Node.val <= 100
Examples
Example 1
Input
root = [1,2,2,3,4,4,3]Output
trueExample 2
Input
root = [1,2,2,null,3,null,3]Output
falseApproaches
1. BFS level compare
Collect each level and check it is a palindrome.
- Time
- O(n)
- Space
- O(n)
// Walk level by level, push vals including nulls, check level === level.reverse()Tradeoff:
2. Mirror DFS
Recurse on (left.left, right.right) and (left.right, right.left).
- 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:
CircleCI-specific tips
CircleCI graders care about clean recursion bases — sloppy null handling here kills your symmetry signal.
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 CircleCI interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →