剑指offer面试题33:二叉搜索树的后序遍历序列

2019-01-07  本文已影响0人  灰化肥发黑会挥发

题目:输入一个数组,判断该数组是不是某搜索二叉树的后序遍历结果。

public class VerifySequence {
    public boolean VerifySequenceOfBST(int[] sequence,int length){
        if(sequence.length==0||length<=0) return false;
        int root = sequence[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 = VerifySequenceOfBST(sequence,i);
        boolean right = true;
        if(j<length-1) right = VerifySequenceOfBST(sequence,j);
        return  left&&right;
    }
}
上一篇下一篇

猜你喜欢

热点阅读