GO
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
* 1. 根节点自己
* 2. 根节点+左子树
* 3. 根节点+右子树
* 4. 根节点+左子树+右子树
*/
import(
"math"
)
func maxPathSum(root *TreeNode) int {
maxInt := math.MinInt32
dfs(root,&maxInt)
return maxInt
}
func dfs(root *TreeNode,maxInt *int) int {
if root == nil{
return 0
}
left := max(0,dfs(root.Left,maxInt))
right := max(0,dfs(root.Right,maxInt))
*maxInt = max(*maxInt,root.Val + left + right)
return max(left,right) + root.Val
}
func max(a,b int) int{
if a > b{
return a
}
return b
}