-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0501.cpp
More file actions
34 lines (33 loc) · 897 Bytes
/
Copy path0501.cpp
File metadata and controls
34 lines (33 loc) · 897 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
30
31
32
33
34
class Solution {
public:
vector<int> findMode(TreeNode *root) {
stack<TreeNode *> st;
int maxi = INT_MIN, cnt = 0, prev = -1;
vector<int> res;
while (true) {
while (root) {
st.push(root);
root = root->left;
}
if (st.empty()) break;
root = st.top(), st.pop();
if (root->val != prev) {
if (cnt >= maxi) {
if (cnt > maxi) res.clear();
maxi = cnt;
res.push_back(prev);
}
prev = root->val;
cnt = 1;
} else
cnt++;
root = root->right;
}
if (cnt >= maxi) {
if (cnt > maxi) res.clear();
maxi = cnt;
res.push_back(prev);
}
return res;
}
};