ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 98. Validate Binary Search Tree

2022-01-09 02:01:25  阅读:183  来源: 互联网

标签:Binary Search right TreeNode val int Tree rightMin root


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.

Example1:

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

Example2:

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

所谓的搜索二叉树,就是一颗二叉树上每个子树的左子树的值都小于它,它的右子树都数值都大于它。根据这个定义,我们可以想到两个方法。
方法一:
二叉树中序遍历(左根右), 得到一个数组,这个数组单调递增,那么根据定义,就一定是搜索二叉树。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        List<Integer> rst = new ArrayList<>();
        helper(root, rst);
        return isIncrease(rst);
    }
    
    private void helper(TreeNode root, List<Integer> rst) {
        if (root == null) {
            return;
        }
        helper(root.left, rst);
        rst.add(root.val);
        helper(root.right, rst);
    }
    
    private boolean isIncrease(List<Integer> rst) {
        for (int i = 0; i < rst.size() - 1; i++) {
            if (rst.get(i) >= rst.get(i + 1)) {
                return false;
            }
        }
        return true;
    }
}

第二个思路,就是用基本的递归。但是因为需要判断每层左子树是不是都小于根,右子树是不是都大于根,所以我们需要三个信息。
1)是不是平衡 => 左边的子树是不是平衡+右边子树是不是平衡+该层是不是平衡
2)左子树的最大值 => 只要最大值小于根就所有都小于
3)右子树的最小值 => 只要最小值大于根就所有都大于

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Info {
    int leftMax;
    int rightMin;
    boolean isValid;
    Info(int leftMax, int rightMin, boolean isValid) {
        this.leftMax = leftMax;
        this.rightMin = rightMin;
        this.isValid = isValid;
    }
}
class Solution {
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        Info rst = helper(root);
        return rst.isValid;
    }
    
    private Info helper(TreeNode root) {
        if (root == null) {
            return null;
        }
        if (root.left == null && root.right == null) {
            return new Info(root.val, root.val, true);
        }
        
     
        Info l = helper(root.left); 
        Info r = helper(root.right);

        int leftMax = root.val;
        int rightMin = root.val;
        boolean isValid = true;
        if ( l != null) {
            isValid = l.leftMax < root.val && l.isValid && isValid;
            leftMax = Math.max(l.leftMax, leftMax); 
            rightMin = Math.min(l.rightMin, rightMin);
        }
        if (r != null) {
            isValid = r.rightMin > root.val && r.isValid && isValid;
            rightMin = Math.min(r.rightMin, rightMin);
            leftMax = Math.max(r.leftMax, leftMax);
        } 
        return new Info(leftMax, rightMin, isValid);
    }
}

标签:Binary,Search,right,TreeNode,val,int,Tree,rightMin,root
来源: https://www.cnblogs.com/codingEskimo/p/15780168.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有