Java语言: ValidateBST
01 public boolean isValidBST(TreeNode root) {
02 return isValidBSTHelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
03 }
04
05 public boolean isValidBSTHelper(TreeNode root, int min, int max)
06 {
07 if(root==null) return true;
08 if(root.val<=min || root.val>=max)
09 return false;
10 return isValidBSTHelper(root.left, min, root.val) && isValidBSTHelper(root.right, root.val, max);
11 }
02 return isValidBSTHelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
03 }
04
05 public boolean isValidBSTHelper(TreeNode root, int min, int max)
06 {
07 if(root==null) return true;
08 if(root.val<=min || root.val>=max)
09 return false;
10 return isValidBSTHelper(root.left, min, root.val) && isValidBSTHelper(root.right, root.val, max);
11 }
没有评论:
发表评论