程序员的一生时间90%是用在编程上,而剩余的10%是活在世界上。
import java.util.*;
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence==null||sequence.length==0){
return false;
}
int length=sequence.length;
int root=sequence[length-1];
int i=0;
for(i=0;i<length-1;i++){
if(sequence[i]>root){
break;
}
}
int j=i;
for(j=i;j<length-1;j++){
if(sequence[j]<root){
return false;
}
}
boolean left=true;
if(i>0){
left=VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,i));
}
boolean right=true;
if(j<length-1){
right=VerifySquenceOfBST(Arrays.copyOfRange(sequence,i,j+1));
}
return (left&right);
}
}
题目描述
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。import java.util.*;
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence==null||sequence.length==0){
return false;
}
int length=sequence.length;
int root=sequence[length-1];
int i=0;
for(i=0;i<length-1;i++){
if(sequence[i]>root){
break;
}
}
int j=i;
for(j=i;j<length-1;j++){
if(sequence[j]<root){
return false;
}
}
boolean left=true;
if(i>0){
left=VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,i));
}
boolean right=true;
if(j<length-1){
right=VerifySquenceOfBST(Arrays.copyOfRange(sequence,i,j+1));
}
return (left&right);
}
}