Skip to content

Commit 84b8195

Browse files
committed
feat: add another solution to lc problem: no.98
1 parent ec3fcf1 commit 84b8195

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

solution/0000-0099/0098.Validate Binary Search Tree/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ class Solution {
106106
}
107107
```
108108

109+
### **JavaScript**
110+
111+
```js
112+
/**
113+
* Definition for a binary tree node.
114+
* function TreeNode(val, left, right) {
115+
* this.val = (val===undefined ? 0 : val)
116+
* this.left = (left===undefined ? null : left)
117+
* this.right = (right===undefined ? null : right)
118+
* }
119+
*/
120+
/**
121+
* @param {TreeNode} root
122+
* @return {boolean}
123+
*/
124+
var isValidBST = function (root) {
125+
let isValidBSTRec = function (root, min, max) {
126+
if (!root) {
127+
return true;
128+
}
129+
if (root.val <= min || root.val >= max) {
130+
return false;
131+
}
132+
return isValidBSTRec(root.left, min, root.val) && isValidBSTRec(root.right, root.val, max);
133+
}
134+
return isValidBSTRec(root, -Infinity, Infinity);
135+
};
136+
```
137+
109138
### **...**
110139

111140
```

solution/0000-0099/0098.Validate Binary Search Tree/README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,35 @@ class Solution {
9898
}
9999
```
100100

101+
### **JavaScript**
102+
103+
```js
104+
/**
105+
* Definition for a binary tree node.
106+
* function TreeNode(val, left, right) {
107+
* this.val = (val===undefined ? 0 : val)
108+
* this.left = (left===undefined ? null : left)
109+
* this.right = (right===undefined ? null : right)
110+
* }
111+
*/
112+
/**
113+
* @param {TreeNode} root
114+
* @return {boolean}
115+
*/
116+
var isValidBST = function (root) {
117+
let isValidBSTRec = function (root, min, max) {
118+
if (!root) {
119+
return true;
120+
}
121+
if (root.val <= min || root.val >= max) {
122+
return false;
123+
}
124+
return isValidBSTRec(root.left, min, root.val) && isValidBSTRec(root.right, root.val, max);
125+
}
126+
return isValidBSTRec(root, -Infinity, Infinity);
127+
};
128+
```
129+
101130
### **...**
102131

103132
```

solution/0000-0099/0098.Validate Binary Search Tree/Solution.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,29 @@ var inOrderTraverse = function (node, arr) {
2626
inOrderTraverse(node.right, arr);
2727
}
2828
};
29+
30+
//Solution 2
31+
/**
32+
* Definition for a binary tree node.
33+
* function TreeNode(val, left, right) {
34+
* this.val = (val===undefined ? 0 : val)
35+
* this.left = (left===undefined ? null : left)
36+
* this.right = (right===undefined ? null : right)
37+
* }
38+
*/
39+
/**
40+
* @param {TreeNode} root
41+
* @return {boolean}
42+
*/
43+
var isValidBST = function (root) {
44+
let isValidBSTRec = function (root, min, max) {
45+
if (!root) {
46+
return true;
47+
}
48+
if (root.val <= min || root.val >= max) {
49+
return false;
50+
}
51+
return isValidBSTRec(root.left, min, root.val) && isValidBSTRec(root.right, root.val, max);
52+
}
53+
return isValidBSTRec(root, -Infinity, Infinity);
54+
};

0 commit comments

Comments
 (0)