Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Created February 5, 2023 09:35
Show Gist options
  • Save jordanrios94/41b86d9b81e51ecbbb71a372536be0a0 to your computer and use it in GitHub Desktop.
Save jordanrios94/41b86d9b81e51ecbbb71a372536be0a0 to your computer and use it in GitHub Desktop.
/*
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