Created
February 5, 2023 09:35
-
-
Save jordanrios94/41b86d9b81e51ecbbb71a372536be0a0 to your computer and use it in GitHub Desktop.
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
/* | |
Given a node of a binary search tree, validate the binary search tree. | |
- Ensure that every node's left hand child is less than the parent node's value | |
- Ensure that every node's right hand child is greater than the parent | |
*/ | |
function validate(node, min = null, max = null) { | |
if (max !== null && node.data > max) { | |
return false; | |
} | |
if (min !== null && node.data < min) { | |
return false; | |
} | |
if (node.left && !validate(node.left, min, node.data)) { | |
return false; | |
} | |
if (node.right && !validate(node.right, node.data, max)) { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment