-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMaxFrequentSubtreeSum.cpp
More file actions
32 lines (31 loc) · 966 Bytes
/
MaxFrequentSubtreeSum.cpp
File metadata and controls
32 lines (31 loc) · 966 Bytes
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxi=-1;
map<int,int>m; // map of how many times a particular sum occurs
int dfs(TreeNode* root){
if(root==NULL) //
return 0;
int total=dfs(root->left)+dfs(root->right)+root->val;
maxi=max(maxi,++m[total]); // it stores how many times the maximum frequency occured
return total; // the total of that tree
}
vector<int> findFrequentTreeSum(TreeNode* root) {
dfs(root);
vector<int>ans; // final vector array
for(auto &i : m){ // map which store the frequency
if(i.second==maxi){
ans.push_back(i.first); // push the ans in the final array
}
}
return ans; // returning the vector
}
};