func maxDepth(root *TreeNode) int {
if root==nil{
return 0
}
return max(maxDepth(root.Left),maxDepth(root.Right))+1
}
法一:深度优先搜索、递归
解题思路:
func maxDepth(root *TreeNode) int {
if root ==nil{
return 0
}
left := maxDepth(root.Left)
fmt.Println(left)
right := maxDepth(root.Right)
fmt.Println(right)
if left >= right{
return left+1
}else{
return right+1
}
}