• BST means, left node value should be smaller than node’s value and
  • right node value should be greater than node’s value
  • A BST supports operations like search, insert, delete, floor, ceil, greater, smaller, etc in O(h) time where h is height of the BST.
class Node {
	constructor(value, left, right) {
		this.value = value;
		this.left = left || null;
		this.right = right || null;
	}
}
/** BST means, left node value should be smaller than node's value
right node value should be greater than node's value
Approach:
-if node data is less than 0 OR node data is greater than MAX_VALUE -> it is not BST
-- if left data is than 0 OR node data - 1 is greater than MAX_VALUE
**/
function checkBST(node, min, max) {
    if (node == null) {
        return true;
    } else if (node.data < min || node.data > max) {
        return false;
    } else {
        return checkBST(node.left, min, node.data - 1) && checkBST(node.right, node.data + 1, max);
    }
}
 
const root = new Node(5, null, null);
const twoVal = new Node(2);
const sixVal = new Node(6);
const oneVal = new Node(1);
sixVal.left = oneVal;
root.left = twoVal;
root.right = sixVal;
console.log(checkBST(root));