Continuing the discussion from freeCodeCamp Challenge Guide: Check if Tree is Binary Search Tree:
Solution 3 (Click to Show/Hide)
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
}
function isBinarySearchTree(tree) {
let node = tree.root;
let isBinary = (root) => {
if(root === null) return true;
if( root.left !== null && root.left.value >= root.value ) return false;
if(root.right !== null && root.right.value < root.value) return false;
return isBinary(root.left) && isBinary(root.right);
}
return isBinary(node);
}