import java.util.Arrays;
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length<=0) return false;
//[0,index])左子树,[index,sequence.length-1)为右子树
int index = sequence.length-1;
for(int i=0;i<(sequence.length-1);i++)
{
if(sequence[i]>sequence[sequence.length-1])
{
index = i;
break;
}
}
for(int j=index;j<sequence.length;j++)
{
if(sequence[j]<sequence[sequence.length-1])
return false;
}
boolean left = true;
if(index>0)
left = VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,index));
boolean right = true;
if(index<sequence.length-1)
right = VerifySquenceOfBST(Arrays.copyOfRange(sequence,index,sequence.length-1));
return left && right;
}
}
33、二叉搜索树的后序遍历序列
最新推荐文章于 2023-03-19 11:29:26 发布