其他分享
首页 > 其他分享> > [LeetCode98] 验证二叉搜索树

[LeetCode98] 验证二叉搜索树

作者:互联网

解法

package com.wangxiaohu;

public class LeetCode98 {

    /**
     * 题目:98. 验证二叉搜索树
     * leetcode:https://leetcode-cn.com/problems/validate-binary-search-tree/
     *
     * @param root
     * @return
     */
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, null, null);
    }

    public boolean isValidBST(TreeNode root, TreeNode min, TreeNode max) {
        if (root == null) {
            return true;
        }

        // 应该满足:min.val < root.val < max.val
        if (min != null && root.val <= min.val) {
            return false;
        }

        if (max != null && root.val >= max.val) {
            return false;
        }

        // 限定左子树的最大值是 root,右子树的最小值是 root
        return isValidBST(root.left, min, root) && isValidBST(root.right, root, max);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}

标签:LeetCode98,right,TreeNode,val,验证,二叉,return,null,root
来源: https://blog.csdn.net/qq_36734216/article/details/121714259