Skip to content

Instantly share code, notes, and snippets.

@trevor-atlas
Created December 12, 2018 02:38
Show Gist options
  • Save trevor-atlas/45bd128868da724af3d02a19b8352223 to your computer and use it in GitHub Desktop.
Save trevor-atlas/45bd128868da724af3d02a19b8352223 to your computer and use it in GitHub Desktop.
Validate BST
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} node
* @return {boolean}
*/
const isValidBST = (node) =>
validateNode(node, -Infinity, Infinity);
const validateNode = (node, min, max) => {
if (node == null) return true;
if (node.val <= min || node.val >= max) {
return false;
}
return validateNode(node.left, min, node.val) && validateNode(node.right, node.val, max);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment