59
59
# self.left = left
60
60
# self.right = right
61
61
class Solution :
62
- pre = None
63
62
def isValidBST (self , root : TreeNode) -> bool :
64
- if not root:
63
+ prev = float (' -inf' )
64
+
65
+ def dfs (root ):
66
+ nonlocal prev
67
+ if root is None :
68
+ return True
69
+ if not dfs(root.left):
70
+ return False
71
+ if prev >= root.val:
72
+ return False
73
+ prev = root.val
74
+ if not dfs(root.right):
75
+ return False
65
76
return True
66
- if not self .isValidBST(root.left):
67
- return False
68
- if self .pre is not None and self .pre >= root.val:
69
- return False
70
- self .pre = root.val
71
- if not self .isValidBST(root.right):
72
- return False
73
- return True
77
+
78
+ return dfs(root)
74
79
```
75
80
76
81
### ** Java**
@@ -94,18 +99,102 @@ class Solution:
94
99
* }
95
100
*/
96
101
class Solution {
97
- private Integer pre = null ;
102
+ private Integer prev;
103
+
98
104
public boolean isValidBST (TreeNode root ) {
99
- if (root == null ) return true ;
100
- if (! isValidBST(root. left)) return false ;
101
- if (pre != null && pre >= root. val) return false ;
102
- pre = root. val;
103
- if (! isValidBST(root. right)) return false ;
105
+ prev = null ;
106
+ return dfs(root);
107
+ }
108
+
109
+ private boolean dfs (TreeNode root ) {
110
+ if (root == null ) {
111
+ return true ;
112
+ }
113
+ if (! dfs(root. left)) {
114
+ return false ;
115
+ }
116
+ if (prev != null && prev >= root. val) {
117
+ return false ;
118
+ }
119
+ prev = root. val;
120
+ if (! dfs(root. right)) {
121
+ return false ;
122
+ }
104
123
return true ;
105
124
}
106
125
}
107
126
```
108
127
128
+ ### ** C++**
129
+
130
+ ``` cpp
131
+ /* *
132
+ * Definition for a binary tree node.
133
+ * struct TreeNode {
134
+ * int val;
135
+ * TreeNode *left;
136
+ * TreeNode *right;
137
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
138
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
139
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
140
+ * };
141
+ */
142
+ class Solution {
143
+ public:
144
+ TreeNode* prev;
145
+
146
+ bool isValidBST(TreeNode* root) {
147
+ prev = nullptr;
148
+ return dfs(root);
149
+ }
150
+
151
+ bool dfs (TreeNode* root) {
152
+ if (!root) return true;
153
+ if (!dfs(root->left)) return false;
154
+ if (prev && prev->val >= root->val) return false;
155
+ prev = root;
156
+ if (!dfs(root->right)) return false;
157
+ return true;
158
+ }
159
+ };
160
+ ```
161
+
162
+ ### **Go**
163
+
164
+ ```go
165
+ /**
166
+ * Definition for a binary tree node.
167
+ * type TreeNode struct {
168
+ * Val int
169
+ * Left *TreeNode
170
+ * Right *TreeNode
171
+ * }
172
+ */
173
+ func isValidBST(root *TreeNode) bool {
174
+ var prev *TreeNode
175
+
176
+ var dfs func(root *TreeNode) bool
177
+ dfs = func(root *TreeNode) bool {
178
+ if root == nil {
179
+ return true
180
+ }
181
+ if !dfs(root.Left) {
182
+ return false
183
+ }
184
+ if prev != nil && prev.Val >= root.Val {
185
+ return false
186
+ }
187
+ prev = root
188
+ if !dfs(root.Right) {
189
+ return false
190
+ }
191
+ return true
192
+ }
193
+
194
+ return dfs(root)
195
+ }
196
+ ```
197
+
109
198
### ** JavaScript**
110
199
111
200
``` js
@@ -121,20 +210,77 @@ class Solution {
121
210
* @param {TreeNode} root
122
211
* @return {boolean}
123
212
*/
124
- var isValidBST = function (root ) {
125
- let isValidBSTRec = function (root , min , max ) {
213
+ var isValidBST = function (root ) {
214
+ let prev = null ;
215
+
216
+ let dfs = function (root ) {
126
217
if (! root) {
127
218
return true ;
128
219
}
129
- if (root .val <= min || root .val >= max) {
220
+ if (! dfs (root .left )) {
221
+ return false ;
222
+ }
223
+ if (prev && prev .val >= root .val ) {
130
224
return false ;
131
225
}
132
- return isValidBSTRec (root .left , min, root .val ) && isValidBSTRec (root .right , root .val , max);
226
+ prev = root;
227
+ if (! dfs (root .right )) {
228
+ return false ;
229
+ }
230
+ return true ;
133
231
}
134
- return isValidBSTRec (root, - Infinity , Infinity );
232
+
233
+ return dfs (root);
135
234
};
136
235
```
137
236
237
+ ### ** C#**
238
+
239
+ ``` cs
240
+ /**
241
+ * Definition for a binary tree node.
242
+ * public class TreeNode {
243
+ * public int val;
244
+ * public TreeNode left;
245
+ * public TreeNode right;
246
+ * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
247
+ * this.val = val;
248
+ * this.left = left;
249
+ * this.right = right;
250
+ * }
251
+ * }
252
+ */
253
+ public class Solution {
254
+ private TreeNode prev ;
255
+
256
+ public bool IsValidBST (TreeNode root ) {
257
+ prev = null ;
258
+ return dfs (root );
259
+ }
260
+
261
+ private bool dfs (TreeNode root ) {
262
+ if (root == null )
263
+ {
264
+ return true ;
265
+ }
266
+ if (! dfs (root .left ))
267
+ {
268
+ return false ;
269
+ }
270
+ if (prev != null && prev .val >= root .val )
271
+ {
272
+ return false ;
273
+ }
274
+ prev = root ;
275
+ if (! dfs (root .right ))
276
+ {
277
+ return false ;
278
+ }
279
+ return true ;
280
+ }
281
+ }
282
+ ```
283
+
138
284
### ** ...**
139
285
140
286
```
0 commit comments