var max int = 0 /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func diameterOfBinaryTree(root *TreeNode) int { max = 0 postorder(root, &max) return } func postorder(root *TreeNode, max *int) int { if root == nil { return -1 } l := postorder(root.Left, max) + 1 r := postorder(root.Right, max) + 1 if *max < l+r { *max = l + r } if l < r { return r } return l }