-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-508-Most-Frequent-Subtree-Sum.java
More file actions
36 lines (32 loc) · 1.08 KB
/
LeetCode-508-Most-Frequent-Subtree-Sum.java
File metadata and controls
36 lines (32 loc) · 1.08 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// DFS, similar to: https://leetcode.com/problems/find-mode-in-binary-search-tree/
public int[] findFrequentTreeSum(TreeNode root) {
if (root == null) return new int[0];
Map<Integer, Integer> map = new HashMap<>();
int[] max = new int[] {0};
dfs(root, map, max);
List<Integer> res = new ArrayList<Integer>();
for (int key : map.keySet()) {
if (map.get(key) == max[0]) {
res.add(key);
}
}
return res.stream().mapToInt(i -> i).toArray();
}
private int dfs(TreeNode root, Map<Integer, Integer> map, int[] max) {
if (root == null) return 0;
int sum = dfs(root.left, map, max) + dfs(root.right, map, max) + root.val;
map.put(sum, map.getOrDefault(sum, 0) + 1);
max[0] = Math.max(max[0], map.get(sum));
return sum;
}
}