-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path508.cpp
More file actions
29 lines (27 loc) · 795 Bytes
/
Copy path508.cpp
File metadata and controls
29 lines (27 loc) · 795 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
class Solution {
public:
unordered_map<int, int> sums;
vector<int> findFrequentTreeSum(TreeNode *root) {
CountSum(root);
vector<int> treeSum;
int time = 0;
for (auto sum : sums) {
if (time < sum.second) {
time = sum.second;
treeSum.clear();
treeSum.push_back(sum.first);
} else if (time == sum.second)
treeSum.push_back(sum.first);
}
return treeSum;
}
int CountSum(TreeNode *node) {
if (!node)
return 0;
int &&leftSum = CountSum(node->left);
int &&rightSum = CountSum(node->right);
int &¤tSum = leftSum + rightSum + node->val;
++sums[currentSum];
return currentSum;
}
};