46
46
47
47
<!-- 这里可写通用的实现逻辑 -->
48
48
49
- 中序遍历,若是一个有效的二叉搜索树,那么遍历到的序列应该是单调递增的。所以只要比较判断遍历到的当前数是否 ` >= ` 上一个数即可。
49
+ ** 方法一:递归**
50
+
51
+ 中序遍历,若是一个有效的二叉搜索树,那么遍历到的序列应该是单调递增的。所以只要比较判断遍历到的当前数是否大于上一个数即可。
52
+
53
+ 或者考虑以 ` root ` 为根的子树,所有节点的值是否都在合法范围内,递归判断即可。
54
+
55
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中节点的数量。
50
56
51
57
<!-- tabs:start -->
52
58
62
68
# self.left = left
63
69
# self.right = right
64
70
class Solution :
65
- def isValidBST (self , root : TreeNode) -> bool :
71
+ def isValidBST (self , root : Optional[ TreeNode] ) -> bool :
66
72
def dfs (root ):
67
73
nonlocal prev
68
74
if root is None :
@@ -80,6 +86,25 @@ class Solution:
80
86
return dfs(root)
81
87
```
82
88
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
+
83
108
### ** Java**
84
109
85
110
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -127,6 +152,39 @@ class Solution {
127
152
}
128
153
```
129
154
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
+
130
188
### ** C++**
131
189
132
190
``` cpp
@@ -161,6 +219,32 @@ public:
161
219
};
162
220
```
163
221
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
+
164
248
### ** Go**
165
249
166
250
``` go
@@ -197,6 +281,31 @@ func isValidBST(root *TreeNode) bool {
197
281
}
198
282
```
199
283
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
+
200
309
### ** JavaScript**
201
310
202
311
``` js
@@ -236,6 +345,33 @@ var isValidBST = function (root) {
236
345
};
237
346
```
238
347
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
+
239
375
### ** C#**
240
376
241
377
``` cs
@@ -283,6 +419,37 @@ public class Solution {
283
419
}
284
420
```
285
421
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
+
286
453
### ** ...**
287
454
288
455
```
0 commit comments