Source: https://leetcode.com/problems/validate-binary-search-tree/

Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node key. The right subtree of a node contains only nodes with keys greater than the node key. Both the left and right subtrees must also be binary search trees.

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
// Keep track of the min and max that a node can be in
const isValidBSTHelper = (root, min, max) => {
// if root is null, it's valid so we return null
if (root === null) {
return true;
}
// if root is not null and there is a min node and the root's val <= min node's val, return false
if (min !== null && root.val <= min.val) {
return false;
}
// if root is not null and there is a max node and the root's val >= max node's val, return false
if (max !== null && root.val >= max.val) {
return false;
}
// recursively check if the left subtree is valid bst and right subtree is valid bst
return isValidBSTHelper(root.left, min, root) && isValidBSTHelper(root.right, root, max);
};
return isValidBSTHelper(root, null, null);
};