98. Validate Binary Search Tree
注意转换类型 别忘了加long
1 class Solution { 2 public boolean isValidBST(TreeNode root) { 3 return helper(root, (long)Integer.MAX_VALUE + 1, (long)Integer.MIN_VALUE - 1); //万一有node就是maxvalue 所以要+1 min同理 4 5 } 6 7 public boolean helper(TreeNode root, long max, long min) { 8 if(root == null) return true; 9 return ((root.val > min) && (root.val < max) && (helper(root.left, Math.min(max, root.val), min)) && (helper(root.right, max, Math.max(min, root.val)))); 10 } 11 }