【LeetCode】98. 验证二叉搜索树(中等)——代码随想录算法训练营Day20

题目链接:98. 验证二叉搜索树

题目描述

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

有效 二叉搜索树定义如下:

  • 节点的左子树只包含 小于 当前节点的数。
  • 节点的右子树只包含 大于 当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入:root = [2,1,3]
输出:true

示例 2:

输入:root = [5,1,4,null,null,3,6]
输出:false
解释:根节点的值是 5 ,但是右子节点的值是 4 。

提示:

  • 树中节点数目范围在[1, 104] 内
  • -231 <= Node.val <= 231 - 1

文章讲解:代码随想录

视频讲解:你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树_哔哩哔哩_bilibili

题解1:递归法

思路:二叉搜索树的中序遍历序列一定是一个递增序列,利用这个方法来判断是否是二叉搜索树。

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
    let num = -Math.MAX_VALUE;
    const inorder = function (node) {
        if (!node) {
            return true;
        }
        const left = inorder(node.left); // 左
        // 中
        if (node.val <= num) {
            return false;
        }
        num = node.val;
        const right = inorder(node.right); // 右
        return left && right;
    }
    return inorder(root);
};

分析:时间复杂度为 O(n),空间复杂度为 O(logn)。

题解2:递归法——双指针优化

思路:使用一个指针进行二叉树的中序遍历,另一个指针指向遍历的前一个元素,用双指针指向的元素进行比较。

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
    let pre = null;
    const inorder = function (node) {
        if (!node) {
            return true;
        }
        const left = inorder(node.left); // 左
        // 中
        if (pre && node.val <= pre.val) {
            return false;
        }
        pre = node;
        const right = inorder(node.right); // 右
        return left && right;
    }
    return inorder(root);
};

分析:时间复杂度为 O(n),空间复杂度为 O(logn)。

题解3:迭代法

思路:使用中序遍历的迭代法解此题。

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
    let pre = null, cur = root;
    const stack = [];
    while (stack.length > 0 || cur) {
        if (cur) {
            stack.push(cur);
            cur = cur.left; // 左
        } else {
            cur = stack.pop();
            // 中
            if (pre && cur.val <= pre.val) {
                return false;
            } else {
                pre = cur;
            }
            cur = cur.right; // 右
        }
    }
    return true;
};

分析:时间复杂度为 O(n),空间复杂度为 O(logn)。

收获

为了利用二叉搜索树的特性,应该使用中序遍历。

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晴雪月乔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值