Leetcode Golang 104. Maximum Depth of Binary Tree.go

2019-04-12 11:11:03 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412882

思路

递归获得左右子树的深度,返回最大值 1

code

代码语言:javascript复制
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
}

0 人点赞