13. Symmetric Tree
easyAsked at ChimeDetermine whether a binary tree is a mirror of itself around its center.
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). Return true if symmetric and false otherwise.
Constraints
The number of nodes is in the range [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. Compare full tree to its mirror
Build a mirrored copy of the tree, then compare the two structurally.
- Time
- O(n)
- Space
- O(n)
function mirror(node) {
if (!node) return null;
return { val: node.val, left: mirror(node.right), right: mirror(node.left) };
}
// then deep-compare(root, mirror(root))Tradeoff:
2. Recursive twin pointers
Recursively check that left.val equals right.val and that left.left mirrors right.right and left.right mirrors right.left.
- Time
- O(n)
- Space
- O(h)
function isSymmetric(root) {
function isMirror(a, b) {
if (!a && !b) return true;
if (!a || !b) return false;
return a.val === b.val
&& isMirror(a.left, b.right)
&& isMirror(a.right, b.left);
}
return isMirror(root, root);
}Tradeoff:
Chime-specific tips
Chime uses symmetric-tree drills as a proxy for whether you can reason about mirrored ledger entries (debit/credit twins), so name the invariant out loud.
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 Chime interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →