## 题目描述

C++实现：

``````class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence) {
if(sequence.size() == 0)
return false;

int t = sequence.size(), i =0;

while(t --)
{
while(sequence[i ++] < sequence[t]);
while(sequence[i ++] > sequence[t]);

if(i < t)
return false;
i = 0;
}

return true;
}
};``````
python实现：
``````# -*- coding:utf-8 -*-
class Solution:
def VerifySquenceOfBST(self, sequence):
# write code here
t = len(sequence)

if t == 0:
return False

i = 0
t = t - 1
while t:
while sequence[i] < sequence[t]:
i = i + 1
while sequence[i] > sequence[t]:
i = i + 1

if i == t:
i = 0
t = t - 1
else:
return False
return True``````

``````class Solution {
public:
bool judge(vector<int> a, int start, int root)
{
if(start >= root)
return true;

int i = 0;
for(i = start; i < root; i ++)
if(a[i] > a[root])
break;

for(int j = i; j < root; j ++)
if(a[j] < a[root])
return false;

return judge(a, start, i - 1) && judge(a, i, root - 1);
}

bool VerifySquenceOfBST(vector<int> sequence) {
if(sequence.size() == 0)
return false;
return judge(sequence, 0, sequence.size() - 1);
}
};``````

0条评论