Skip to content

Commit e363de9

Browse files
add a solution for 1339
1 parent 86aaefb commit e363de9

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

src/main/java/com/fishercoder/solutions/secondthousand/_1339.java

+65-6
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,88 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5+
import java.util.ArrayList;
6+
import java.util.HashMap;
57
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
610
import java.util.Set;
711

812
public class _1339 {
913
public static class Solution1 {
1014
public int maxProduct(TreeNode root) {
1115
Set<Long> set = new HashSet<>();
12-
int total = dfs(root, set);
16+
long total = postOrder(root, set);
1317
long result = 0L;
1418
for (long sum : set) {
1519
result = Math.max(result, sum * (total - sum));
1620
}
1721
return (int) (result % 1000000007);
1822
}
1923

20-
private int dfs(TreeNode root, Set<Long> set) {
24+
private long postOrder(TreeNode root, Set<Long> set) {
2125
if (root == null) {
2226
return 0;
2327
}
24-
root.val += dfs(root.left, set);
25-
root.val += dfs(root.right, set);
26-
set.add((long) root.val);
27-
return root.val;
28+
long leftSum = postOrder(root.left, set);
29+
long rightSum = postOrder(root.right, set);
30+
long sum = root.val + leftSum + rightSum;
31+
set.add(sum);
32+
return sum;
2833
}
2934
}
35+
36+
public static class Solution2 {
37+
/**
38+
* My completely original solution, but much more verbose and uses more extra space.
39+
*/
40+
public int maxProduct(TreeNode root) {
41+
Map<TreeNode, Long> sumMap = new HashMap<>();
42+
long totalSum = postOrderBuildSumMap(root, sumMap);
43+
sumMap.put(root, totalSum);
44+
List<long[]> productList = new ArrayList<>();
45+
postOrderBuildProductList(root, sumMap, productList, sumMap.get(root));
46+
long result = 0L;
47+
double modulo = Math.pow(10, 9) + 7;
48+
for (long[] p : productList) {
49+
long product = p[0] * p[1];
50+
result = Math.max(result, product);
51+
}
52+
return (int) (result % modulo);
53+
}
54+
55+
private void postOrderBuildProductList(TreeNode root, Map<TreeNode, Long> sumMap, List<long[]> productList, Long total) {
56+
if (root == null) {
57+
return;
58+
}
59+
if (root.left == null && root.right == null) {
60+
return;
61+
}
62+
postOrderBuildProductList(root.left, sumMap, productList, total);
63+
postOrderBuildProductList(root.right, sumMap, productList, total);
64+
if (root.left != null) {
65+
//cut off left child
66+
long leftSum = sumMap.get(root.left);
67+
long remainder = total - leftSum;
68+
productList.add(new long[]{leftSum, remainder});
69+
}
70+
if (root.right != null) {
71+
long rightSum = sumMap.get(root.right);
72+
long remainder = total - rightSum;
73+
productList.add(new long[]{rightSum, remainder});
74+
}
75+
}
76+
77+
private long postOrderBuildSumMap(TreeNode root, Map<TreeNode, Long> sumMap) {
78+
if (root == null) {
79+
return 0L;
80+
}
81+
long leftSum = postOrderBuildSumMap(root.left, sumMap);
82+
long rightSum = postOrderBuildSumMap(root.right, sumMap);
83+
long sum = leftSum + rightSum + root.val;
84+
sumMap.put(root, sum);
85+
return sum;
86+
}
87+
88+
}
3089
}

src/test/java/com/fishercoder/secondthousand/_1339Test.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions.secondthousand._1339;
6-
import org.junit.BeforeClass;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
88

99
import java.util.Arrays;
1010

11-
import static org.junit.Assert.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
1212

1313
public class _1339Test {
1414
private static _1339.Solution1 solution1;
15+
private static _1339.Solution2 solution2;
1516
private static TreeNode root;
1617

17-
@BeforeClass
18-
public static void setup() {
18+
@BeforeEach
19+
public void setup() {
1920
solution1 = new _1339.Solution1();
21+
solution2 = new _1339.Solution2();
2022
}
2123

2224
@Test
@@ -25,4 +27,10 @@ public void test1() {
2527
assertEquals(110, solution1.maxProduct(root));
2628
}
2729

30+
@Test
31+
public void test2() {
32+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6));
33+
assertEquals(110, solution2.maxProduct(root));
34+
}
35+
2836
}

0 commit comments

Comments
 (0)