讨论/《二叉搜索树》 - 验证二叉搜索树/
《二叉搜索树》 - 验证二叉搜索树
共 1 个回复

Java

/**
 * 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) {
        return mid(root,Long.MIN_VALUE,Long.MAX_VALUE);
    }
    public boolean mid(TreeNode root, long min, long max){
        if(root == null)return true;
        if(root.val<=min || root.val>=max){
            return false;
        }
        return mid(root.left,min,root.val) && mid(root.right,root.val,max);
    }
}
1