098_验证二叉搜索树
小于 1 分钟
098_验证二叉搜索树中等
class Solution {
public boolean isValidBST(TreeNode root) {
return inSpread(root,(long)Integer.MIN_VALUE - 1,(long)Integer.MAX_VALUE + 1);
}
boolean inSpread(TreeNode node,long left,long right){
if(node.val <= left || node.val >= right){
return false;
}
if(node.left != null){
if(!inSpread(node.left, left, node.val)) return false;
}
if(node.right != null){
if(!inSpread(node.right, node.val, right)) return false;
}
return true;
}
}
递归判断左右节点是否满足条件,传入参数为左右区间的值,小于等于left或大于等于right都是false
Powered by Waline v2.15.5