Skip to content

Instantly share code, notes, and snippets.

@sorindragan
Created March 19, 2018 16:16
Show Gist options
  • Save sorindragan/af14eb2bf42ee1fa66ab9b684cd0052a to your computer and use it in GitHub Desktop.
Save sorindragan/af14eb2bf42ee1fa66ab9b684cd0052a to your computer and use it in GitHub Desktop.
A function that checks if a given tree is a BST.
boolean checkBST(Node root) {
return checkHelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
boolean checkHelper(Node node, int min_left, int max_right) {
if (node == null) {
return true;
}
if (node.data <= min_left || node.data >= max_right) {
return false;
}
return checkHelper(node.left, min_left, node.data) && checkHelper(node.right, node.data, max_right);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment