-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindFrequentTreeSum.java
More file actions
63 lines (57 loc) · 1.88 KB
/
findFrequentTreeSum.java
File metadata and controls
63 lines (57 loc) · 1.88 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;
/**
* Author : WindAsMe
* File : findFrequentTreeSum.java
* Time : Create on 18-8-7
* Location : ../Home/JavaForLeeCode2/findFrequentTreeSum.java
* Function : LeetCode No.508
*/
public class findFrequentTreeSum {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
private static int[] findFrequentTreeSumResult(TreeNode root) {
if (root == null)
return new int[0];
// Map utility and process the data
Map<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
dfs(root, map);
int occur = 0;
for (Map.Entry<Integer, Integer> temp : map.entrySet())
occur = temp.getValue() > occur ? temp.getValue() : occur;
for (Map.Entry<Integer, Integer> temp : map.entrySet()) {
if (temp.getValue() == occur)
list.add(temp.getKey());
}
int[] ans = new int[list.size()];
for (int i = 0; i < list.size(); i++)
ans[i] = list.get(i);
return ans;
}
private static void dfs(TreeNode node, Map<Integer, Integer> map) {
if (node != null) {
int[] sum = new int[1];
account(node, sum);
map.merge(sum[0], 1, (a, b) -> a + b);
dfs(node.left, map);
dfs(node.right, map);
}
}
private static void account(TreeNode node, int[] sum) {
if (node != null) {
sum[0] += node.val;
account(node.left, sum);
account(node.right, sum);
}
}
public static void main(String[] args) {
TreeNode node = new TreeNode(5);
node.left = new TreeNode(2);
node.right = new TreeNode(-5);
System.out.println(Arrays.toString(findFrequentTreeSumResult(node)));
}
}