11. Symmetric Tree
easyAsked at BoxCheck whether a binary tree mirrors itself around the center — Box uses this pattern when validating mirrored backup-tree consistency between primary and DR regions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Constraints
Number of nodes in [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. Mirror and compare
Build a mirrored copy and check equality.
- Time
- O(n)
- Space
- O(n)
function mirror(t) { return !t ? null : { val: t.val, left: mirror(t.right), right: mirror(t.left) }; }
// then compare original with mirrorTradeoff:
2. Recursive pair check
Recurse left.left vs right.right and left.right vs 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:
Box-specific tips
Box graders want the helper recursion pattern that walks two subtrees in lockstep — they reuse it in their DR-region replica-consistency checker.
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 Box interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →