Leetcode会员尊享面试100题:255.验证二叉搜索树的前序遍历序列

给定一个 无重复元素 的整数数组 preorder , 如果它是以二叉搜索树的先序遍历排列 ,返回 true 。

示例 1:

复制代码
输入: preorder = [5,2,1,3,6]
输出: true

示例 2:

复制代码
输入: preorder = [5,2,6,1,3]
输出: false

提示:

  • 1 <= preorder.length <= 104
  • 1 <= preorder[i] <= 104
  • preorder 中 无重复元素

**进阶:**您能否使用恒定的空间复杂度来完成此题?

直接上代码,不懂请留言或私信

java 复制代码
class Solution {
    public boolean verifyPreorder(int[] preorder) {
        if(preorder.length == 1) {
            return true;
        }
        Info info = getInfo(preorder, 0, preorder.length - 1);
        return info.isBST;
    }
    /**当前树的范围是start~end,计算这棵树的Info信息 */
    public Info getInfo(int[] preorder, int start, int end) {
        if(start == end) {
            /**根据二叉搜索树的定义,如果只有一个节点就是 */
            return new Info(preorder[start], preorder[start], true);
        }
        /**拿到root的值 */
        int rootVal = preorder[start];
        /**现在我们还没有遍历不知道左右子树的情况,就以自己考虑设置minValue、maxValue还有isBST */
        int minValue = rootVal;
        int maxValue = rootVal;
        boolean isBST = true;
        /**这种情况只有右子树 */
        if(preorder[start + 1] > preorder[start]) {
            /**左子树为空,我们不用做任何关于左子树的比较*/
            Info info = getInfo(preorder, start + 1, end);
            /**统一写法*/
            minValue = Math.min(info.minValue, rootVal);
            maxValue = Math.max(rootVal, info.maxValue);
            isBST = info.isBST && rootVal < info.minValue;
            return new Info(minValue, maxValue, isBST);
        } else {
            /**有左子树,找一下左子树的终点的下一个位置*/
            int leftEndPost = searchFirstG(preorder, start + 1, end, rootVal);
            if(leftEndPost == -1) {
                /**只有左子树,剩下所有都是左子树的信息*/
                Info info = getInfo(preorder, start + 1, end);
                minValue = Math.min(info.minValue, rootVal);
                maxValue = Math.max(rootVal, info.maxValue);
                isBST = info.isBST && rootVal > info.maxValue;
                return new Info(minValue, maxValue, isBST);
            } else {
                /**左右子树都有,需要获取两颗子树的信息,左子树丛start+1~leftEndPost-1,右子树从leftEndPost~end */
                Info leftInfo = getInfo(preorder, start + 1, leftEndPost - 1);
                Info rightInfo = getInfo(preorder, leftEndPost, end);
                minValue = Math.min(leftInfo.minValue, Math.min(rootVal, rightInfo.minValue));
                maxValue = Math.min(leftInfo.maxValue, Math.min(rootVal, rightInfo.maxValue));
                /**这里需要左右子树都判断,左子树最大值必须比rootVal小,右子树最小值必须比rootVal大 */
                isBST = leftInfo.isBST && rightInfo.isBST && leftInfo.maxValue < rootVal && rightInfo.minValue > rootVal;
                return new Info(minValue, maxValue, isBST);
            }
        }
    }
    /**获取第一个大于root值的元素,使用二分查找*/
    public int searchFirstG(int[] arr, int start, int end, int rootVal) {
        if(start > end) {
            return -1;
        }
        /**先定义为-1,如果没有查找到最后就是-1,如果查找到了大雨rootVal的就更新成满足条件的最小的 */
        int index = -1;
        while(start <= end) {
            int mid = start + (end - start)/2;
            /**根据题意,无重复元素,所以这里直接判断大于就行,一般的二分是需要写>= */
            if(arr[mid] > rootVal) {
                /**找到了一个大于等于的,但是这未必是最终的答案,需要继续往小了找 */
                index = mid;
                end = mid - 1;
            } else {
                /**范围错了,如果有这个值肯定在前面 */
                start = mid + 1;
            }
        }
        return index;
    }
}

/**根据二叉搜索树的特性:根节点比它左子树的任何节点都大
比它右子树的任何节点都小,并且左右子树都是二叉搜索树,所以我们需要左右子树的以下信息:最大值、最小值、是否为二叉搜索树*/
class Info{
    int minValue;
    int maxValue;
    boolean isBST;
    public Info(int minValue, int maxValue, boolean isBST) {
        this.minValue = minValue;
        this.maxValue = maxValue;
        this.isBST = isBST;
    }
}

运行结果:

相关推荐
我叫黑大帅12 小时前
换尺寸、转个角度就认不出同一张图?让 16×16 灰度网格做感知去重
后端·面试·php
Moment15 小时前
AI 写代码越来越快,程序员为什么反而更累了
前端·后端·面试
郝学胜-神的一滴16 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger16 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
青少儿编程课堂19 小时前
蓝桥杯真题解析:三维偏序的CDQ分治(全国青少年信息素养大赛与电子学会编程考级)
c++·python·算法·bfs·信息学竞赛
Navigator_Z19 小时前
LeetCode //C - 1323. Maximum 69 Number
c语言·算法·leetcode
JianZhen✓20 小时前
后端进阶 13 问:CI/CD、Prisma、ORM、ER 图、SSE、索引事务、Nest 生命周期一次讲透(附面试答法)
ci/cd·面试·职场和发展
geovindu20 小时前
python:Lasso
开发语言·后端·python·算法
AAA修煤气灶刘哥21 小时前
同一个模型,为什么还要专门练一遍怎么用工具?
人工智能·后端·算法