/
MaximumProductOfSplittedBinaryTree.java
66 lines (56 loc) · 1.58 KB
/
MaximumProductOfSplittedBinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
// https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/
class Solution {
public int maxProduct(TreeNode root) {
MaxTracker tracker = new MaxTracker(totalSum(root));
maxProduct(root, tracker);
return tracker.getMax();
}
int totalSum(TreeNode root) {
if (root == null) {
return 0;
}
return root.val + totalSum(root.left) + totalSum(root.right);
}
int maxProduct(TreeNode root, MaxTracker tracker) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
tracker.test(root.val);
return root.val;
}
int subtreeSum = maxProduct(root.left, tracker) + maxProduct(root.right, tracker) + root.val;
tracker.test(subtreeSum);
return subtreeSum;
}
class MaxTracker {
final int totalSum;
long max;
MaxTracker(int totalSum) {
this.totalSum = totalSum;
}
void test(int sum) {
int rem = totalSum - sum;
long product = 1L * sum * rem;
max = Math.max(max, product);
}
int getMax() {
return (int) (max % 1000000007);
}
}
}