Algorithm-Day40-Symmetric-Tree-lc-101
🧩 Problem Description
Given the root
of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
💬 Example
1 | Input: root = [1,2,2,3,4,4,3] |
Symmetric ✅
💡 Approach 1: Recursive Comparison
We define a helper function isMirror(left, right)
to check whether two subtrees are mirror images of each other.
🔢 Java Code (Recursive)
1 | class Solution { |
💡 Approach 2: Iterative (BFS with Queue)
We can also use a queue to compare nodes level by level.
🔢 Java Code (Iterative)
1 | class Solution { |
⏱ Time and Space Complexity
- Time Complexity: O(n) — visit each node once
- Space Complexity:
- Recursive: O(h)
- Iterative: O(w) for queue
✍️ Summary
- Use recursion for elegant, clear code.
- Iterative is slightly more verbose but stack-safe.
- Understand the mirror condition:
left.left == right.right
left.right == right.left
Often paired with
lc-100
,lc-226
, and other binary tree symmetry/mirroring problems.