题目:
题解:
func diameterOfBinaryTree(root *TreeNode) int {var (dfs func(node *TreeNode) int // node节点深度ans int)dfs = func(node *TreeNode) int {//边界if node == nil {return -1}//求左右子树深度leftDepth := dfs(node.Left)rightDepth := dfs(node.Right)//维护经过node最长路径ans = max(ans, leftDepth+rightDepth+2)//最大深度return max(leftDepth, rightDepth) + 1}dfs(root)return ans
}