diff options
author | terminaldweller <thabogre@gmail.com> | 2022-12-10 07:34:17 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-12-10 07:34:17 +0000 |
commit | 5ef0e4444e14887b819e0d373a9a7d6f2f33d4e8 (patch) | |
tree | e27c12b92ca877bda7dcca1380b9af2e657ebe18 /1339/main.go | |
parent | 1026 (diff) | |
download | leetcode-5ef0e4444e14887b819e0d373a9a7d6f2f33d4e8.tar.gz leetcode-5ef0e4444e14887b819e0d373a9a7d6f2f33d4e8.zip |
1339
Diffstat (limited to '1339/main.go')
-rw-r--r-- | 1339/main.go | 46 |
1 files changed, 46 insertions, 0 deletions
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") +} |