-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify-preorder-sequence-in-binary-search-tree.cpp
38 lines (35 loc) · 1.37 KB
/
verify-preorder-sequence-in-binary-search-tree.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
class Solution {
public:
// ????amazing?????https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/discuss/68142/Java-O(n)-and-O(1)-extra-space
// ??????????????????????
// ???????? https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/discuss/68185/C++-easy-to-understand-solution-with-thought-process-and-detailed-explanation
bool verifyPreorder1(vector<int>& preorder) {
int peek=INT_MIN;
stack<int> s;
for(int i=0;i<preorder.size();i++){
if(peek>preorder[i]){
// cout<<preorder[i]<<" "<<peek<<endl;
return false;
}
while(!s.empty() && preorder[i]>s.top()){ // ?????while?????????preorder[i]??????peek?????
peek=s.top(); // ?????preorder[i]??????????
s.pop();
}
// ??preorder[i]?peek????,??????peek?????
s.push(preorder[i]); // ?????????
}
return true;
}
// follow up???????:??????preorder????????
bool verifyPreorder(vector<int>& preorder) {
int peek=INT_MIN;
int i=-1;
for(auto p:preorder){
if(peek>p) return false;
while(i>=0 && p>preorder[i])
peek=preorder[i--];
preorder[++i]=p;
}
return true;
}
};