-
Notifications
You must be signed in to change notification settings - Fork 1
/
MostFrequentSubtreeSum.java
48 lines (40 loc) · 1.4 KB
/
MostFrequentSubtreeSum.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
package com.smlnskgmail.jaman.leetcodejava.medium;
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// https://leetcode.com/problems/most-frequent-subtree-sum/
public class MostFrequentSubtreeSum {
private final TreeNode input;
private int freq = 0;
public MostFrequentSubtreeSum(TreeNode input) {
this.input = input;
}
public int[] solution() {
Map<Integer, Integer> values = new HashMap<>();
fillValues(input, values);
List<Integer> mostFreq = new ArrayList<>();
for (Map.Entry<Integer, Integer> value : values.entrySet()) {
if (value.getValue() == freq) {
mostFreq.add(value.getKey());
}
}
int[] result = new int[mostFreq.size()];
for (int i = 0; i < mostFreq.size(); i++) {
result[i] = mostFreq.get(i);
}
return result;
}
private int fillValues(TreeNode node, Map<Integer, Integer> values) {
if (node != null) {
int left = fillValues(node.left, values);
int right = fillValues(node.right, values);
int sum = node.val + left + right;
values.put(sum, values.getOrDefault(sum, 0) + 1);
freq = Math.max(freq, values.get(sum));
return sum;
}
return 0;
}
}