11. Symmetric Tree
easyAsked at GoDaddyDetermine whether a binary tree is a mirror image of itself about its center.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the root of a binary tree, check whether the tree is a mirror of itself (symmetric around its center). The left subtree must be a mirror reflection of the right subtree.
Constraints
1 <= nodes <= 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. BFS level lists
BFS, capture each level (with nulls), check palindrome.
- Time
- O(n)
- Space
- O(n)
// BFS recording values level by level then check palindrome per levelTradeoff:
2. Recursive mirror compare
Recurse on (left.left,right.right) and (left.right,right.left).
- Time
- O(n)
- Space
- O(h)
function isSymmetric(root) {
const mirror = (a, b) => {
if (!a && !b) return true;
if (!a || !b || a.val !== b.val) return false;
return mirror(a.left, b.right) && mirror(a.right, b.left);
};
return !root || mirror(root.left, root.right);
}Tradeoff:
GoDaddy-specific tips
GoDaddy uses tree symmetry to test reasoning about mirrored DNS configurations between a primary and secondary nameserver pair.
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 GoDaddy interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →