check if tree is binary or not code example
Example: check if binary search tree is valid
class BTNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
/**
*
* @param {BTNode} tree
* @returns {Boolean}
*/
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;
};