Skip to content

Commit 67abcc7

Browse files
authored
Update 98.validate-binary-search-tree.md
validate-binary-search-tree 定义法添加js代码实现
1 parent eb94452 commit 67abcc7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

problems/98.validate-binary-search-tree.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class Solution {
185185

186186
### 定义法
187187

188-
* 语言支持:C++,Python3, Java
188+
* 语言支持:C++,Python3, Java, JS
189189

190190
C++ Code:
191191

@@ -311,6 +311,34 @@ class Solution {
311311
}
312312
```
313313

314+
JS Code:
315+
316+
```javascript
317+
/**
318+
* Definition for a binary tree node.
319+
* function TreeNode(val) {
320+
* this.val = val;
321+
* this.left = this.right = null;
322+
* }
323+
*/
324+
/**
325+
* @param {TreeNode} root
326+
* @return {boolean}
327+
*/
328+
var isValidBST = function(root) {
329+
if(!root)return true;
330+
return valid(root);
331+
};
332+
333+
function valid(root,min = -Infinity,max=Infinity){
334+
if(!root)return true;
335+
const val = root.val;
336+
if(val<=min)return false;
337+
if(val>=max)return false;
338+
return valid(root.left,min,Math.min(val,max))&&valid(root.right,Math.max(val,min),max)
339+
}
340+
```
341+
314342
## 相关题目
315343

316344
[230.kth-smallest-element-in-a-bst](./230.kth-smallest-element-in-a-bst.md)

0 commit comments

Comments
 (0)