版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412882
递归获得左右子树的深度,返回最大值+1
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
l := maxDepth(root.Left)
r := maxDepth(root.Right)
return max(l, r) + 1
}
func max(x, y int) int {
if x > y {
return x
}
return y
}