二叉搜索树的后序序列

2018-11-03  本文已影响0人  怎样会更好

思路:找住二叉查找树的特点:左子树<根<=右子树 使用分治思想

public class Solution {
   public static boolean VerifySquenceOfBST(int[] sequence) {
        if (sequence.length == 0) {
            return false;
        }
        if (sequence.length == 1) {
            return true;
        }
        return Judge(sequence, 0, sequence.length - 1);
    }

    public static boolean Judge(int[] seq, int start, int end) {
         if (start >= end) {
            return true;
        }
        int i = start;
        while(seq[i]<seq[end]){
            i++;
        }
        for (int j = i; j < end; j++) {
            if (seq[j] < seq[end]) {
                return false;
            }
        }
        return Judge(seq, start, i - 1) && Judge(seq, i, end - 1);
    }
}
上一篇下一篇

猜你喜欢

热点阅读