Skip to content

Commit 2c3a09c

Browse files
committed
feat: add solutions to lc problem: No.0098
No.0098.Validate Binary Search Tree
1 parent 930dae6 commit 2c3a09c

File tree

5 files changed

+339
-11
lines changed

5 files changed

+339
-11
lines changed

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

+169-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
中序遍历,若是一个有效的二叉搜索树,那么遍历到的序列应该是单调递增的。所以只要比较判断遍历到的当前数是否 `>=` 上一个数即可。
49+
**方法一:递归**
50+
51+
中序遍历,若是一个有效的二叉搜索树,那么遍历到的序列应该是单调递增的。所以只要比较判断遍历到的当前数是否大于上一个数即可。
52+
53+
或者考虑以 `root` 为根的子树,所有节点的值是否都在合法范围内,递归判断即可。
54+
55+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中节点的数量。
5056

5157
<!-- tabs:start -->
5258

@@ -62,7 +68,7 @@
6268
# self.left = left
6369
# self.right = right
6470
class Solution:
65-
def isValidBST(self, root: TreeNode) -> bool:
71+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
6672
def dfs(root):
6773
nonlocal prev
6874
if root is None:
@@ -80,6 +86,25 @@ class Solution:
8086
return dfs(root)
8187
```
8288

89+
```python
90+
# Definition for a binary tree node.
91+
# class TreeNode:
92+
# def __init__(self, val=0, left=None, right=None):
93+
# self.val = val
94+
# self.left = left
95+
# self.right = right
96+
class Solution:
97+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
98+
def dfs(root, l, r):
99+
if root is None:
100+
return True
101+
if root.val <= l or root.val >= r:
102+
return False
103+
return dfs(root.left, l, root.val) and dfs(root.right, root.val, r)
104+
105+
return dfs(root, -inf, inf)
106+
```
107+
83108
### **Java**
84109

85110
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -127,6 +152,39 @@ class Solution {
127152
}
128153
```
129154

155+
```java
156+
/**
157+
* Definition for a binary tree node.
158+
* public class TreeNode {
159+
* int val;
160+
* TreeNode left;
161+
* TreeNode right;
162+
* TreeNode() {}
163+
* TreeNode(int val) { this.val = val; }
164+
* TreeNode(int val, TreeNode left, TreeNode right) {
165+
* this.val = val;
166+
* this.left = left;
167+
* this.right = right;
168+
* }
169+
* }
170+
*/
171+
class Solution {
172+
public boolean isValidBST(TreeNode root) {
173+
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
174+
}
175+
176+
private boolean dfs(TreeNode root, long l, long r) {
177+
if (root == null) {
178+
return true;
179+
}
180+
if (root.val <= l || root.val >= r) {
181+
return false;
182+
}
183+
return dfs(root.left, l, root.val) && dfs(root.right, root.val, r);
184+
}
185+
}
186+
```
187+
130188
### **C++**
131189

132190
```cpp
@@ -161,6 +219,32 @@ public:
161219
};
162220
```
163221
222+
```cpp
223+
/**
224+
* Definition for a binary tree node.
225+
* struct TreeNode {
226+
* int val;
227+
* TreeNode *left;
228+
* TreeNode *right;
229+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
230+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
231+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
232+
* };
233+
*/
234+
class Solution {
235+
public:
236+
bool isValidBST(TreeNode* root) {
237+
return dfs(root, LONG_MIN, LONG_MAX);
238+
}
239+
240+
bool dfs(TreeNode* root, long long l, long long r) {
241+
if (!root) return true;
242+
if (root->val <= l || root->val >= r) return false;
243+
return dfs(root->left, l, root->val) && dfs(root->right, root->val, r);
244+
}
245+
};
246+
```
247+
164248
### **Go**
165249

166250
```go
@@ -197,6 +281,31 @@ func isValidBST(root *TreeNode) bool {
197281
}
198282
```
199283

284+
```go
285+
/**
286+
* Definition for a binary tree node.
287+
* type TreeNode struct {
288+
* Val int
289+
* Left *TreeNode
290+
* Right *TreeNode
291+
* }
292+
*/
293+
func isValidBST(root *TreeNode) bool {
294+
return dfs(root, math.MinInt64, math.MaxInt64)
295+
}
296+
297+
func dfs(root *TreeNode, l, r int64) bool {
298+
if root == nil {
299+
return true
300+
}
301+
v := int64(root.Val)
302+
if v <= l || v >= r {
303+
return false
304+
}
305+
return dfs(root.Left, l, v) && dfs(root.Right, v, r)
306+
}
307+
```
308+
200309
### **JavaScript**
201310

202311
```js
@@ -236,6 +345,33 @@ var isValidBST = function (root) {
236345
};
237346
```
238347

348+
```js
349+
/**
350+
* Definition for a binary tree node.
351+
* function TreeNode(val, left, right) {
352+
* this.val = (val===undefined ? 0 : val)
353+
* this.left = (left===undefined ? null : left)
354+
* this.right = (right===undefined ? null : right)
355+
* }
356+
*/
357+
/**
358+
* @param {TreeNode} root
359+
* @return {boolean}
360+
*/
361+
var isValidBST = function (root) {
362+
function dfs(root, l, r) {
363+
if (!root) {
364+
return true;
365+
}
366+
if (root.val <= l || root.val >= r) {
367+
return false;
368+
}
369+
return dfs(root.left, l, root.val) && dfs(root.right, root.val, r);
370+
}
371+
return dfs(root, -Infinity, Infinity);
372+
};
373+
```
374+
239375
### **C#**
240376

241377
```cs
@@ -283,6 +419,37 @@ public class Solution {
283419
}
284420
```
285421

422+
```cs
423+
/**
424+
* Definition for a binary tree node.
425+
* public class TreeNode {
426+
* public int val;
427+
* public TreeNode left;
428+
* public TreeNode right;
429+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
430+
* this.val = val;
431+
* this.left = left;
432+
* this.right = right;
433+
* }
434+
* }
435+
*/
436+
public class Solution {
437+
public bool IsValidBST(TreeNode root) {
438+
return dfs(root, long.MinValue, long.MaxValue);
439+
}
440+
441+
public bool dfs(TreeNode root, long l, long r) {
442+
if (root == null) {
443+
return true;
444+
}
445+
if (root.val <= l || root.val >= r) {
446+
return false;
447+
}
448+
return dfs(root.left, l, root.val) && dfs(root.right, root.val, r);
449+
}
450+
}
451+
```
452+
286453
### **...**
287454

288455
```

0 commit comments

Comments
 (0)