-
Notifications
You must be signed in to change notification settings - Fork 1
/
501FindModeInBST.cpp
48 lines (45 loc) · 1.23 KB
/
501FindModeInBST.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
// 501. find mode in Binary Search Tree
class Solution {
public:
vector<int> findMode(TreeNode* root) {
vector<int> res;
int maxNum = 0;
unordered_map<int, int> map;
preorder(root, maxNum, map);
for (auto entry: map) {
if (entry.second == maxNum)
res.push_back(entry.first);
}
return res;
}
void preorder(TreeNode* root, int& maxNum, unordered_map<int, int>& map) {
if (!root) return ;
maxNum = max(maxNum, ++map[root->val]);
preorder(root->left, maxNum, map);
preorder(root->right, maxNum, map);
}
};
class Solution {
public:
vector<int> findMode(TreeNode* root) {
vector<int> res;
int mx = 0, cnt = 1;
TreeNode* pre = NULL;
inorder(root, pre, cnt, mx, res);
return res;
}
void inorder(TreeNode* node, TreeNode* pre, int& cnt, int& mx, vector<int>& res) {
if (!node) return;
inorder(node->left, pre, cnt, mx, res);
if (pre) {
cnt = (node->val == pre->val) ? cnt + 1 : cnt;
}
if (cnt >= mx) {
if (cnt > mx) res.clear();
res.push_back(node->val);
mx = cnt;
}
pre = node;
inorder(node->right, pre, cnt, mx, res);
}
};