-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht508. Most Frequent Subtree Sum.cpp
More file actions
54 lines (44 loc) · 1.05 KB
/
Copy patht508. Most Frequent Subtree Sum.cpp
File metadata and controls
54 lines (44 loc) · 1.05 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
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// void bianRoot(unordered_map<int,int>& map,TreeNode* root){
// int cum = 0;
// if(root){
// cum+=root->val;
// cum+=bian(map,root->left);
// cum+=bian(map,root->right);
// map[cum]++;
// }
// }
int bian(unordered_map<int,int>& map,TreeNode* root){
if(!root){
return 0;
}
int temV = root->val;
temV+=bian(map,root->left);
temV+=bian(map,root->right);
//bianRoot(map,root);
map[temV]++;
return temV;
}
vector<int> findFrequentTreeSum(TreeNode* root) {
unordered_map<int,int> map;
bian(map,root);
vector<int> res;
int maxt=0;
for(auto it:map){
if(it.second>maxt){
res.clear();
maxt=it.second;
res.push_back(it.first);
}else if(it.second==maxt){
res.push_back(it.first);
}
}
return res;
}