[Easy] 101. Symmetric Tree
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Thinking Process
- Mirror Comparison: Compare left subtree with right subtree as mirrors
a->left↔b->right(outer nodes)a->right↔b->left(inner nodes)
- Trees have no cycles — recursion is natural.
- Combine results from left and right subtrees at each node.
- Base case is usually
null; height drives stack space.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Recursive DFS (this problem) | O(n) | O(h) stack | Natural for trees and graphs |
| Iterative DFS (stack) | O(n) | O(n) | Avoid recursion depth limits |
| DFS with memoization | O(n) | O(n) | Overlapping subproblems on graphs |
| Backtracking DFS | O(2^n) typical | O(n) | Enumerate choices with pruning |
Examples
Example 1:
Input: root = [1,2,2,3,4,4,3]
Output: true
Tree structure:
1
/ \
2 2
/ \ / \
3 4 4 3
Example 2:
Input: root = [1,2,2,null,3,null,3]
Output: false
Tree structure:
1
/ \
2 2
\ \
3 3
Constraints
- The number of nodes in the tree is in the range
[1, 1000]. -100 <= Node.val <= 100
Common Mistakes
- Empty tree:
root = []→ returntrue - Single node:
root = [1]→ returntrue - Symmetric tree:
[1,2,2,3,4,4,3]→ returntrue - Asymmetric structure:
[1,2,2,null,3,null,3]→ returnfalse - Asymmetric values:
[1,2,2,3,4,5,3]→ returnfalse -
One child:
[1,2,null]→ returnfalse - Wrong comparison order: Comparing
a->leftwithb->leftinstead ofb->right// WRONG: return isMirror(a->left, b->left) && isMirror(a->right, b->right); // ❌ This checks if trees are identical, not mirrors - Not handling null correctly: Accessing values before null check
- Wrong logic operator: Using OR instead of AND
- Missing value check: Only checking structure, not values
- Comparing same side: Forgetting to cross-compare (left with right)
Related Problems
- LC 101: Symmetric Tree - This problem
- LC 100: Same Tree - Check if two trees are identical
- LC 226: Invert Binary Tree - Mirror a binary tree
- LC 572: Subtree of Another Tree - Check if subtree exists
- LC 104: Maximum Depth of Binary Tree - Find maximum depth
- LC 110: Balanced Binary Tree - Check if tree is balanced
Key Takeaways
- Mirror Comparison: Compare left subtree with right subtree as mirrors
- Cross Comparison:
a->left↔b->right(outer nodes)a->right↔b->left(inner nodes)
- Three Base Cases: Both null, one null, or value mismatch
- AND Logic: Both comparisons must succeed for symmetry
- Empty Tree: Empty tree is symmetric
References
- LC 101: Symmetric Tree on LeetCode
- LeetCode Discuss — LC 101: Symmetric Tree
- LeetCode Editorial (may require premium)