LeetCode 98. Validate Binary Search Tree(验证二叉搜索树)

331 阅读1分钟

leetcode.com/problems/va…

Discuss:www.cnblogs.com/grandyang/p…

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

  • 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.

Example 1:

Input: root = [2,1,3]
Output: true

Example 2:

Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.

Constraints:

  • The number of nodes in the tree is in the range [1, 104].

  • -231 <= Node.val <= 231 - 1

解法一:

这道验证二叉搜索树有很多种解法,可以利用它本身的性质来做,即左<根<右,也可以通过利用中序遍历结果为有序数列来做,下面我们先来看最简单的一种,就是利用其本身性质来做,初始化时带入系统最大值和最小值,在递归过程中换成它们自己的节点值,用 long 代替 int 就是为了包括 int 的边界条件,代码如下:

class Solution {
    fun isValidBST(root: TreeNode?): Boolean {
        return recursive(root, Long.MIN_VALUE, Long.MAX_VALUE)
    }

    private fun recursive(root: TreeNode?, min: Long?, max: Long?): Boolean {
        if (root == null) {
            return true
        }

        if (min != null && min >= root.`val`) {
            return false
        }

        if (max != null && max <= root.`val`) {
            return false
        }

        return recursive(root.left, min, root.`val`.toLong()) && recursive(root.right, root.`val`.toLong(), max)
    }
}

解法二:

这题实际上简化了难度,因为有的时候题目中的二叉搜索树会定义为左<=根<右,而这道题设定为一般情况左<根<右,那么就可以用中序遍历来做。因为如果不去掉左=根这个条件的话,那么下边两个数用中序遍历无法区分:

image.png

它们的中序遍历结果都一样,但是左边的是 BST,右边的不是 BST。去掉等号的条件则相当于去掉了这种限制条件。下面来看使用中序遍历来做,这种方法思路很直接,通过中序遍历将所有的节点值存到一个数组里,然后再来判断这个数组是不是有序的,代码如下:

class Solution {
    fun isValidBST(root: TreeNode?): Boolean {
        val list = mutableListOf<Int>()

        inOrder(root, list)

        for (index in 0 until list.size - 1) {
            if (list[index] >= list[index + 1]) {
                return false
            }
        }
        return true
    }

    private fun inOrder(node: TreeNode?, list: MutableList<Int>) {
        if (node == null) {
            return
        }
        inOrder(node.left, list)
        list.add(node.`val`)
        inOrder(node.right, list)
    }
}

解法三:

下面这种解法跟上面那个很类似,都是用递归的中序遍历,但不同之处是不将遍历结果存入一个数组遍历完成再比较,而是每当遍历到一个新节点时和其上一个节点比较,如果不大于上一个节点那么则返回 false,全部遍历完成后返回 true。代码如下:

class Solution {
    fun isValidBST(root: TreeNode?): Boolean {
        val prev = arrayOfNulls<Int?>(1)
        return inOrder(root, prev)
    }

    private fun inOrder(node: TreeNode?, prev: Array<Int?>): Boolean {
        if (node == null) {
            return true
        }
        val result = inOrder(node.left, prev)
        if (!result) {
            return false
        }
        prev[0]?.let {
            if (node.`val` <= it) {
                return false
            }
        }
        prev[0] = node.`val`
        return inOrder(node.right, prev)
    }
}

解法四:

这道题也可以用非递归来做,需要用到栈或队列,这里用到的是一个双向队列,因为中序遍历可以非递归来实现,所以只要在其上面稍加改动便可,代码如下:

class Solution {
    fun isValidBST(root: TreeNode?): Boolean {
        val arrayDeque = ArrayDeque<TreeNode>()
        var p: TreeNode? = root
        var pre: TreeNode? = null

        while (p != null || !arrayDeque.isEmpty()) {
            while (p != null) {
                arrayDeque.addLast(p)
                p = p.left
            }
            p = arrayDeque.last
            arrayDeque.removeLast()
            pre?.let { pre ->
                p?.let { p ->
                    if (pre.`val` >= p.`val`) {
                        return false
                    }
                }
            }
            pre = p
            p = p.right
        }
        return true
    }
}