algorithm to cheeck if supplies binary tree is also binary search tree code example
Example: check if binary search tree is valid
class BTNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const isBinarySearchTree = (tree) => {
if (tree) {
if (
tree.left &&
(tree.left.value > tree.value || !isBinarySearchTree(tree.left))
) {
return false;
}
if (
tree.right &&
(tree.right.value <= tree.value || !isBinarySearchTree(tree.right))
) {
return false;
}
}
return true;
};