aboutsummaryrefslogtreecommitdiffstats
path: root/124/main.go
diff options
context:
space:
mode:
Diffstat (limited to '124/main.go')
-rw-r--r--124/main.go44
1 files changed, 44 insertions, 0 deletions
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")
+}