forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1339.java
30 lines (26 loc) · 829 Bytes
/
_1339.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.HashSet;
import java.util.Set;
public class _1339 {
public static class Solution1 {
public int maxProduct(TreeNode root) {
Set<Long> set = new HashSet<>();
int total = dfs(root, set);
long result = 0L;
for (long sum : set) {
result = Math.max(result, sum * (total - sum));
}
return (int) (result % 1000000007);
}
private int dfs(TreeNode root, Set<Long> set) {
if (root == null) {
return 0;
}
root.val += dfs(root.left, set);
root.val += dfs(root.right, set);
set.add((long) root.val);
return root.val;
}
}
}