Skip to content

Commit e2a658c

Browse files
add a solution for 508
1 parent 523c705 commit e2a658c

File tree

2 files changed

+54
-16
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+54
-16
lines changed

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_508.java

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fishercoder.solutions.firstthousand;
22

33
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.solutions._Contest;
45

56
import java.util.ArrayList;
67
import java.util.Collections;
@@ -107,9 +108,42 @@ private int dfs(TreeNode root, Map<Integer, Integer> map) {
107108
if (root.right != null) {
108109
rightVal = dfs(root.right, map);
109110
}
110-
int val = leftVal + rightVal + root.val;
111-
map.put(val, map.getOrDefault(val, 0) + 1);
112-
return val;
111+
int subtreeSum = leftVal + rightVal + root.val;
112+
map.put(subtreeSum, map.getOrDefault(subtreeSum, 0) + 1);
113+
return subtreeSum;
114+
}
115+
}
116+
117+
public static class Solution3 {
118+
/**
119+
* Use post-order traversal for this problem as it needs to process subtree first before processing the root.
120+
*/
121+
Map<Integer, Integer> map = new HashMap<>();
122+
123+
public int[] findFrequentTreeSum(TreeNode root) {
124+
postOrder(root);
125+
int mostFreq = -1;
126+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
127+
mostFreq = Math.max(mostFreq, entry.getValue());
128+
}
129+
List<Integer> list = new ArrayList<>();
130+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
131+
if (entry.getValue() == mostFreq) {
132+
list.add(entry.getKey());
133+
}
134+
}
135+
return list.stream().mapToInt(integer -> integer).toArray();
136+
}
137+
138+
private int postOrder(TreeNode root) {
139+
if (root == null) {
140+
return 0;
141+
}
142+
int leftSum = postOrder(root.left);
143+
int rightSum = postOrder(root.right);
144+
int subtreeSum = leftSum + rightSum + root.val;
145+
map.put(subtreeSum, map.getOrDefault(subtreeSum, 0) + 1);
146+
return subtreeSum;
113147
}
114148
}
115149

Diff for: src/test/java/com/fishercoder/firstthousand/_508Test.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,26 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions.firstthousand._508;
6-
import org.junit.Before;
7-
import org.junit.BeforeClass;
8-
import org.junit.Test;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
98

109
import java.util.Arrays;
1110

12-
import static org.junit.Assert.assertArrayEquals;
11+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1312

1413
public class _508Test {
1514
private static _508.Solution1 solution1;
1615
private static _508.Solution2 solution2;
16+
private static _508.Solution3 solution3;
1717
private static int[] expected;
1818
private static int[] actual;
1919
private static TreeNode root;
2020

21-
@BeforeClass
22-
public static void setup() {
21+
@BeforeEach
22+
public void setup() {
2323
solution1 = new _508.Solution1();
2424
solution2 = new _508.Solution2();
25-
}
26-
27-
@Before
28-
public void setupForEachTest() {
29-
expected = new int[]{};
30-
actual = new int[]{};
31-
root = null;
25+
solution3 = new _508.Solution3();
3226
}
3327

3428
@Test
@@ -44,6 +38,10 @@ public void test1() {
4438
actual = solution2.findFrequentTreeSum(root);
4539
Arrays.sort(actual);
4640
assertArrayEquals(expected, actual);
41+
42+
actual = solution3.findFrequentTreeSum(root);
43+
Arrays.sort(actual);
44+
assertArrayEquals(expected, actual);
4745
}
4846

4947
@Test
@@ -55,6 +53,9 @@ public void test2() {
5553

5654
actual = solution2.findFrequentTreeSum(root);
5755
assertArrayEquals(expected, actual);
56+
57+
actual = solution3.findFrequentTreeSum(root);
58+
assertArrayEquals(expected, actual);
5859
}
5960

6061
@Test
@@ -67,5 +68,8 @@ public void test3() {
6768

6869
actual = solution2.findFrequentTreeSum(root);
6970
assertArrayEquals(expected, actual);
71+
72+
actual = solution3.findFrequentTreeSum(root);
73+
assertArrayEquals(expected, actual);
7074
}
7175
}

0 commit comments

Comments
 (0)