-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0508-most-frequent-subtree-sum.js
51 lines (45 loc) · 1.23 KB
/
0508-most-frequent-subtree-sum.js
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
/**
* 508. Most Frequent Subtree Sum
* https://leetcode.com/problems/most-frequent-subtree-sum/
* Difficulty: Medium
*
* Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return
* all the values with the highest frequency in any order.
*
* The subtree sum of a node is defined as the sum of all the node values formed by the subtree
* rooted at that node (including the node itself).
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var findFrequentTreeSum = function(root) {
if (!root) return [];
const map = new Map();
let result = [];
let max = 0;
traverse(root);
for (const [sum, freq] of map) {
if (freq > max) {
max = freq;
result = [sum];
} else if (freq === max) {
result.push(sum);
}
}
return result;
function traverse(node) {
if (!node) return 0;
const sum = node.val + traverse(node.left) + traverse(node.right);
map.set(sum, (map.get(sum) || 0) + 1);
return sum;
}
};