后数组是否是二叉搜索树的后序

    xiaoxiao2021-03-25  77

    public class VerifySquenceOfBST { public static boolean isSquenceOfBST(int [] a,int startId,int rootId){ if(a==null||a.length<=0){ return false; } if(startId<0||rootId<=0){ return true; } int fg1=0,fg2=0; int i=rootId-1; for(;i>=startId;i--){ if(a[i]<a[rootId]){ //第一个小于rootId的值 fg1=1; break; } } int j=i; for(;j>=startId;j--){ if(a[j]>a[rootId]){ // 判断是否有不小于a[rootId]的值 fg2=1; break; } } if(fg2==1){ return false; }else{ return isSquenceOfBST(a,startId,i)&&isSquenceOfBST(a,i+1,rootId-1); } } public static void main(String args[]){ int a[]={2,6,4,8,10,7}; System.out.println(VerifySquenceOfBST.isSquenceOfBST(a, 0, 5)); } }
    转载请注明原文地址: https://ju.6miu.com/read-25107.html

    最新回复(0)