543.E 二叉树的直径
思路
Code
func diameterOfBinaryTreeV1(root *TreeNode) int {
path, _ := helper(root)
return path - 1
}
func helper(root *TreeNode) (int, int) {
if root == nil {
return 0, 0
}
lpath, ldepth := helper(root.Left)
rpath, rdepth := helper(root.Right)
return max(max(lpath, rpath), ldepth+rdepth+1), max(ldepth, rdepth) + 1
}
func max(a, b int) int {
if a < b {
return b
}
return a
}Last updated