12. Symmetric Tree
easyAsked at ExpediaDetermine if 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 (i.e., symmetric around its center).
Constraints
The number of nodes is 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. Inorder list
Inorder traversal then check palindrome (fails on duplicates).
- Time
- O(n)
- Space
- O(n)
// fragile: equal values can fake symmetry
const arr=[]; function dfs(n){if(!n) return arr.push(null);
dfs(n.left);arr.push(n.val);dfs(n.right);} dfs(root);Tradeoff:
2. Mirror recursion
Recursively check that left.left mirrors right.right and left.right mirrors right.left. Expedia uses similar logic to validate balanced bundle structures.
- 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:
Expedia-specific tips
Expedia favors clean recursive solutions; mention how the same pattern applies to verifying paired outbound/inbound legs of round-trip itineraries.
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 Expedia interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →