Created
March 19, 2018 16:16
-
-
Save sorindragan/af14eb2bf42ee1fa66ab9b684cd0052a to your computer and use it in GitHub Desktop.
A function that checks if a given tree is a BST.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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