-
Notifications
You must be signed in to change notification settings - Fork 0
/
508MostFrequentSubtreeSum.java
37 lines (36 loc) · 1.11 KB
/
508MostFrequentSubtreeSum.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
public class Solution {
public int[] findFrequentTreeSum(TreeNode root) {
if(root == null){
return new int[0];
}
Map<Integer,Integer> sums = new HashMap<Integer,Integer>();
getSum(root,sums);
//System.out.print(sums.keySet().size());
int max = 0;
List<Integer> res= new ArrayList<Integer>();
for(Integer key: sums.keySet()){
if(sums.get(key) >max){
res = new ArrayList<Integer>();
res.add(key);
max = sums.get(key);
}else if(sums.get(key)==max){
res.add(key);
}
}
int[] solution = new int[res.size()];
for(int i=0;i<solution.length;i++){
solution[i] = res.get(i);
}
return solution;
}
private int getSum(TreeNode x, Map<Integer,Integer> sums){
if(x == null){
return 0;
}
int left = getSum(x.left,sums);
int right = getSum(x.right,sums);
int sum = left+right+x.val;
sums.put(sum,sums.getOrDefault(sum,0)+1);
return sum;
}
}