LeetCode 之 JavaScript 解答第98题 —— 验证二叉搜索树(Validate Binary Search Tree)


Time:2019/4/24
Title: Vaildata Binary Search Tree
Difficulty: Medium
Author: 小鹿
javascript


题目:Vaildata Binary Search Tree(验证二叉搜索树)

Given a binary tree, determine if it is a valid binary search tree (BST).java

Assume a BST is defined as follows:node

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

给定一个二叉树,判断其是不是一个有效的二叉搜索树。git

假设一个二叉搜索树具备以下特征:github

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

Example 1:算法

Input:
    2
   / \
  1   3
Output: true
复制代码

Example 2:编程

5
   / \
  1   4
     / \
    3   6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value is 5 but its right child's value is 4.
复制代码

Solve:

▉ 问题分析

看到此题的入手点就是上方提出的三点二叉搜索树的三点要求:bash

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

1)以上三点要求最容易解决的就是一个中序遍历,判断遍历出的每一个元素后一个元素是否大于前一个元素,若是不符合条件,那么就不是一个二分搜索树。less

▉ 算法思路

1)定义全局的 boolean 变量,用来返回是否为 二叉搜索树。ui

2)定义一个边界值赋予 max 变量。每遍历一次,若是符合先后大小的要求,就将当前节点的值赋值给 max 变量,用于下一次遍历的结点的大小比较。若是不符合要求,咱们将其布尔变量置为 false。

3)整个过程是用递归来解决的,在理解上仍是有点不符合常规思路的。也是整个问题分析中最重要的一点。

▉ 代码实现
var isValidBST = function(root) {
    // boolean 变量
    let isValidBSTFlag = true;
    // 最大值变量
    let max = -Number.MAX_VALUE;
    const orderSearch = root => {
        // 终止条件(判断当前结点是否为 null)
        if (root) {
            // 中序遍历
            orderSearch(root.left);
            // 判断遍历先后的值是否逐渐升序
            if (root.val > max) {
                // 存储当前结点值,进行下一次比较
                max = root.val;
            } else {
                // 当前节点值小于前一结点值,返回 false
                isValidBSTFlag = false;
            }
            orderSearch(root.right);
        }
    }
    orderSearch(root);
    return isValidBSTFlag;
};
复制代码
欢迎一块儿加入到 LeetCode 开源 Github 仓库,能够向 me 提交您其余语言的代码。在仓库上坚持和小伙伴们一块儿打卡,共同完善咱们的开源小仓库! Github:https://github.com/luxiangqiang/JS-LeetCode 欢迎关注我我的公众号:「一个不甘平凡的码农」,记录了本身一路自学编程的故事。
相关文章
相关标签/搜索