-
Notifications
You must be signed in to change notification settings - Fork 1
/
501+findMode.cpp
69 lines (62 loc) · 1.72 KB
/
501+findMode.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> findMode(TreeNode* root) {
traversal(root);
return res;
}
private:
map<int,int> mp;
vector<int> res;
int count = 0;
void traversal(TreeNode* root) {
if (root == NULL) return;
traversal(root->left);
mp[root->val]++;
if (mp[root->val] > count) {
res.clear();
res.push_back(root->val);
count = mp[root->val];
} else if (mp[root->val] == count) {
res.push_back(root->val);
}
traversal(root->right);
}
};
class Solution {
public:
vector<int> findMode(TreeNode* root) {
vector<int> res;
if (root == NULL) return res;
traversal(root);
vector<pair<int, int>> vec(mp.begin(), mp.end());
sort(vec.begin(), vec.end(), cmp);
res.push_back(vec[0].first);
for (int i = 1; i < vec.size(); i++) {
if (vec[i].second == vec[0].second) res.push_back(vec[i].first);
else break;
}
return res;
}
bool static cmp (const pair<int, int> a, const pair<int, int> b) {
return a.second > b.second ? true : false;
}
private:
map<int,int> mp;
void traversal(TreeNode* root) {
if (root == NULL) return;
traversal(root->left);
mp[root->val]++;
traversal(root->right);
}
};