1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public boolean isValidBST(TreeNode root) { return isValid(root, null, null); } public boolean isValid(TreeNode root,Integer min, Integer max){ if(root == null){ return true; } if(min != null && root.val <= min){ return false; } if(max != null && root.val >= max){ return false; } if(!isValid(root.left, min, root.val)){ return false; } if(!isValid(root.right, root.val, max)){ return false; } return true; } }
|