题目描述
AC代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null)
return true;
return dfs(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
}
boolean dfs(TreeNode root,long minx,long maxx){
if(root==null)
return true;
if(root.val<minx||root.val>maxx)
return false;
return dfs(root.left,minx,root.val-1l)&&dfs(root.right,root.val+1l,maxx);
}
}