Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Last active December 12, 2015 05:09
Show Gist options
  • Save charlespunk/4719970 to your computer and use it in GitHub Desktop.
Save charlespunk/4719970 to your computer and use it in GitHub Desktop.
Implement a function to check is a binary tree is a binary search tree.
public static boolean validate(Node root){
validate(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public static boolean validate(Node root, int mix, int max){
if(root == null) return true;
else if(root.value <= min || root.value > max) return false;
return (validate(root.left, min, root.value) && validate (root.right, root.value, max));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment