代码如下:
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
// Corner Case
if(sequence.length == 0){
return false;
}
return helper(sequence, 0, sequence.length - 1);
}
private boolean helper(int[] sequence, int i, int j) {
if(i >= j){
return true;
}
int p = i;
// Find the first node of right subtree
while(sequence[p] < sequence[j]){
p++;
}
int m = p;
// Check whether the right subtree is correct
while(sequence[p] > sequence[j]){
p++;
}
if(p != j){
return false;
}
return helper(sequence, i, m - 1) && helper(sequence, m , j - 1);
}
}