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 Sam K., 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
Input
root = [1,2,2,3,4,4,3]Output
trueExample 2
Input
root = [1,2,2,null,3,null,3]Output
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.
More Box coding interview questions
- 1. Two Sum
- 2. Valid Parentheses
- 3. Merge Two Sorted Lists
- 4. Remove Duplicates from Sorted Array
- 5. Remove Element
- 6. Search Insert Position
- 7. Plus One
- 8. Merge Sorted Array