124.H 二叉树中的最大路径和
思路
Code
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxPathSum(root *TreeNode) int {
maxSum := math.MinInt
var maxGain func(root *TreeNode) int
maxGain = func(root *TreeNode) int {
if root == nil {
// impossible case
return 0
}
leftGain := max(maxGain(root.Left), 0)
rightGain := max(maxGain(root.Right), 0)
maxSum = max(maxSum, leftGain+root.Val+rightGain)
return root.Val + max(leftGain, rightGain)
}
maxGain(root)
return maxSum
}
func max(a, b int) int {
if a > b {
return a
}
return b
}Last updated