add: 验证二叉搜索树

This commit is contained in:
2020-05-05 18:25:52 +08:00
parent b806ec7959
commit aa83ce62e9
6 changed files with 82 additions and 42 deletions

View File

@ -0,0 +1,16 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
export const isValidBST = function (root, lower = -Infinity, upper = Infinity) {
if (root === null) return true
if (root.val <= lower || root.val >= upper) return false
return isValidBST(root.left, lower, root.val) && isValidBST(root.right, root.val, upper)
}