美文网首页
二叉搜索树的后序序列

二叉搜索树的后序序列

作者: 怎样会更好 | 来源:发表于2018-11-03 11:47 被阅读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);
    }
}

相关文章

网友评论

      本文标题:二叉搜索树的后序序列

      本文链接:https://www.haomeiwen.com/subject/herkxqtx.html