diff options
Diffstat (limited to '1339')
-rw-r--r-- | 1339/go.mod | 3 | ||||
-rw-r--r-- | 1339/main.go | 46 |
2 files changed, 49 insertions, 0 deletions
diff --git a/1339/go.mod b/1339/go.mod new file mode 100644 index 0000000..aa2e740 --- /dev/null +++ b/1339/go.mod @@ -0,0 +1,3 @@ +module 1339 + +go 1.19 diff --git a/1339/main.go b/1339/main.go new file mode 100644 index 0000000..8eed077 --- /dev/null +++ b/1339/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" +) + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func max(a, b int64) int64 { + if a > b { + return int64(a) + } else { + return int64(b) + } +} + +func maxProduct(root *TreeNode) int { + var total int64 + var result int64 + + var sumUnder func(*TreeNode) int64 + sumUnder = func(node *TreeNode) int64 { + var sum int64 + if node == nil { + return 0 + } + + sum = sumUnder(node.Left) + sumUnder(node.Right) + int64(node.Val) + result = max(int64(result), sum*(total-sum)) + + return sum + } + + total = sumUnder(root) + sumUnder(root) + + return int(result % (1_000_000_000 + 7)) +} + +func main() { + fmt.Println("vim-go") +} |