diff options
author | terminaldweller <thabogre@gmail.com> | 2022-12-11 07:18:28 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-12-11 07:18:28 +0000 |
commit | 47e6ccd958e5d9e83a474fd2763e243311d2f516 (patch) | |
tree | 4c67c2a56f0446d9fbe5c224d72570286479f3ca /124 | |
parent | 1339 (diff) | |
download | leetcode-47e6ccd958e5d9e83a474fd2763e243311d2f516.tar.gz leetcode-47e6ccd958e5d9e83a474fd2763e243311d2f516.zip |
124
Diffstat (limited to '124')
-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") +} |