diff options
-rw-r--r-- | 124/go.mod | 3 | ||||
-rw-r--r-- | 124/main.go | 44 |
2 files changed, 47 insertions, 0 deletions
diff --git a/124/go.mod b/124/go.mod new file mode 100644 index 0000000..355f7d1 --- /dev/null +++ b/124/go.mod @@ -0,0 +1,3 @@ +module 124 + +go 1.19 diff --git a/124/main.go b/124/main.go new file mode 100644 index 0000000..fa953f1 --- /dev/null +++ b/124/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "math" +) + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func max(a, b int) int { + if a > b { + return a + } else { + return b + } +} + +func walk(node *TreeNode, sum *int) int { + if node == nil { + return 0 + } + + left := max(0, walk(node.Left, sum)) + right := max(0, walk(node.Right, sum)) + *sum = max(*sum, left+right+node.Val) + + return max(left, right) + node.Val +} + +func maxPathSum(root *TreeNode) int { + sum := math.MinInt + + walk(root, &sum) + + return sum +} + +func main() { + fmt.Println("vim-go") +} |