点击打开链接
class Solution {
public:
map<int, int> m;
void dfs(TreeNode* root){
if(root==NULL) return;
if(root->left !=NULL){
dfs(root->left);
root->val += root->left->val;
}
if(root->right !=NULL){
dfs(root->right);
root->val += root->right->val;
}
m[root->val] +=1;
}
vector<int> findFrequentTreeSum(TreeNode* root) {
vector<int> result;
dfs(root);
int maxn = 0;
for(auto it =m.begin(); it!=m.end(); it++){
maxn = max(maxn, it->second);
}
for(auto it=m.begin(); it!=m.end(); it++){
if(it->second == maxn){
result.push_back(it->first);
}
}
return result;
}
};