Files
interview/16-LeetCode Hot 100/二叉树的最大深度.md

30 lines
471 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 二叉树的最大深度 (Maximum Depth of Binary Tree)
## 题目描述
给定一个二叉树,找出其最大深度。
## 解题思路
### DFS / BFS
## 解法
```go
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
left := maxDepth(root.Left)
right := maxDepth(root.Right)
if left > right {
return left + 1
}
return right + 1
}
```
**复杂度:** O(n) 时间O(h) 空间h 为高度)