Created
December 12, 2018 02:38
-
-
Save trevor-atlas/45bd128868da724af3d02a19b8352223 to your computer and use it in GitHub Desktop.
Validate BST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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