Leetcode-104-111-Max-Min-Depth-BT
🧠 思路总结
- 104: Max depth
- 递归 dfs:
- 空节点返回 0
- 递归调用左子树和右子树
- 返回 max(left, right) + 1
- 递归 dfs:
- 111: Min depth
- root == null → 0
- 只有一边为空 → 非空那边 + 1
- 两边都不空 → min(left, right) + 1
❗ 易错点 / 小坑
- 111: Min depth
- 不能直接用
min(left, right) + 1 - 如果一边是 null,必须走另一边
- 因为最小深度必须到“叶子”,不能走到空就停
- 不能直接用
✅ 代码(Java)
1 | |
1 | |
Leetcode-104-111-Max-Min-Depth-BT
https://powersixxx.github.io/leetcode-blog/2026/01/13/Leetcode-104-111-Max-Min-Depth-BT/