题目:
题解:
class Solution {
unordered_map<int, int> cnt;
int maxCnt = 0;
int dfs(TreeNode *node) {
if (node == nullptr) {
return 0;
}
int sum = node->val + dfs(node->left) + dfs(node->right);
maxCnt = max(maxCnt, ++cnt[sum]);
return sum;
}
public:
vector<int> findFrequentTreeSum(TreeNode *root) {
dfs(root);
vector<int> ans;
for (auto &[s, c]: cnt) {
if (c == maxCnt) {
ans.emplace_back(s);
}
}
return ans;
}
};