add: 验证二叉搜索树
This commit is contained in:
1 parent
b806ec7959
commit
aa83ce62e9
6 files changed
+82
-42
No files matched your search
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user