左子树上界是root.val,下界是Integer.MIN_VALUE;
右子树上界是Integer.MAX_VALU,下界是root.val;
public class Solution {
/**
*
* @param root TreeNode类
* @return bool布尔型
*/
public boolean isValidBST (TreeNode root) {
// write code here
return dfs(root,Integer.MAX_VALUE,Integer.MIN_VALUE);
}
public boolean dfs(TreeNode root , int max , int min){
if(root == null)
return true;
if(root.val >= max || root.val <= min)
return false;
return dfs(root.left , root.val , min) && dfs(root.right , max , root.val);
}
}
京公网安备 11010502036488号