-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmostFrequentSubtreeSum.cpp
More file actions
40 lines (38 loc) · 1.18 KB
/
mostFrequentSubtreeSum.cpp
File metadata and controls
40 lines (38 loc) · 1.18 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
// Source: https://leetcode.com/problems/most-frequent-subtree-sum/
// Author: Miao Zhang
// Date: 2021-02-19
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
unordered_map<int, int> dic;
vector<int> res;
int max_freq = -1;
getSums(root, dic, max_freq, res);
return res;
}
int getSums(TreeNode* root, unordered_map<int, int>& dic, int& max_freq, vector<int>& res) {
if (!root) return 0;
int val = root->val + getSums(root->left, dic, max_freq, res)
+ getSums(root->right, dic, max_freq, res);
int freq = dic[val]++;
if (freq > max_freq) {
max_freq = freq;
res.clear();
}
if (freq == max_freq) {
res.push_back(val);
}
return val;
}
};