Skip to content

Instantly share code, notes, and snippets.

@AlexeiDarmin
Created November 18, 2018 18:40
Show Gist options
  • Save AlexeiDarmin/a82bc5d194535fd9d4b9831b6e51e11a to your computer and use it in GitHub Desktop.
Save AlexeiDarmin/a82bc5d194535fd9d4b9831b6e51e11a to your computer and use it in GitHub Desktop.
Implement a function to validate that a binary tree is a binary search tree.
// Valiate binary tree: Implement a function to validate that a binary tree is a binary search tree.
function validateBST<T>(node: BinaryNode<T>) {
if (!node) return true
if (node.leftChild && node.leftChild.data > node.data) return false
if (node.rightChild && node.rightChild.data < node.data) return false
return validateBST(node.leftChild) && validateBST(node.rightChild)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment