-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path501.cpp
98 lines (86 loc) · 2.6 KB
/
501.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
__________________________________________________________________________________________________
sample 16 ms submission
class Solution {
public:
TreeNode* findright(TreeNode* root){
while(root->right){root=root->right;}
return root;
}
vector<int> findMode(TreeNode* root) {
if(!root){ return {}; }
vector<int> res;
// int last=INT_MAX;
int last=1000;
int max_index=0;
int index=0;
while(root){
if(!root->left){
if(root->val==last){index++;}
else{
if(index==max_index){res.push_back(last);}
else {if(index>max_index){max_index=index;res.clear();res.push_back(last);}}
index=1;
last=root->val;
}
root=root->right;
}else{
TreeNode * p=findright(root->left);
p->right=root;
root=root->left;
p->right->left=NULL;
}
}
if(index==max_index){res.push_back(last);}
else {if(index>max_index){max_index=index;res.clear();res.push_back(last);}}
return res;
}
};
__________________________________________________________________________________________________
sample 23168 kb submission
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> findMode(TreeNode* root) {
int cur = INT_MAX;
int cur_max = 0;
int prev_max = 1;
TreeNode* node = root;
stack<TreeNode*> s;
vector<int> modes;
while(node || !s.empty()){
if(!node){
node = s.top();
s.pop();
if(node->val != cur){
if(cur_max > prev_max){
prev_max = cur_max;
modes = {cur};
}else if(cur_max == prev_max)
modes.push_back(cur);
cur_max = 1;
cur = node->val;
}else
cur_max++;
node = node->right;
}
while(node){
s.push(node);
node = node->left;
}
}
if(cur_max > prev_max)
modes = {cur};
else if(cur_max == prev_max)
modes.push_back(cur);
return modes;
}
};
__________________________________________________________________________________________________