二叉搜索树的后序遍历序列 (java)

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<length-1;i++){
            if(sequence[i]>root){
                break;
            }
        }
        int j=i;
        for(;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(i<length-1)
        right=VerifySquenceOfBST(Arrays.copyOfRange(sequence,i,length-1));
        return (right&&left);
    }
}

 

posted @ 2017-12-23 22:10  Rainydayfmb  阅读(386)  评论(0编辑  收藏  举报