Skip to content

Instantly share code, notes, and snippets.

@sopherwang
Created September 9, 2014 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sopherwang/42cdefa2ddff3cb6e758 to your computer and use it in GitHub Desktop.
Save sopherwang/42cdefa2ddff3cb6e758 to your computer and use it in GitHub Desktop.
Validate Binary Search Tree
public boolean isValidBST(TreeNode root)
{
return isValidate(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean isValidate(TreeNode root, int min, int max)
{
if (root == null)
{
return true;
}
if (min >= root.val || max <= root.val)
{
return false;
}
return isValidate(root.left, min, root.val) && isValidate(root.right, root.val, max);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment