Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f726dfc

Browse files
committed
feat: update solutions to lc problem: No.0098
* Update solutions to lc problem: No.0098.Validate Binary Search Tree * Add new lc problem: No.2047~2050
1 parent 348d3e4 commit f726dfc

File tree

24 files changed

+1175
-158
lines changed

24 files changed

+1175
-158
lines changed

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

Lines changed: 167 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,23 @@
5959
# self.left = left
6060
# self.right = right
6161
class Solution:
62-
pre = None
6362
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
6576
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)
7479
```
7580

7681
### **Java**
@@ -94,18 +99,102 @@ class Solution:
9499
* }
95100
*/
96101
class Solution {
97-
private Integer pre = null;
102+
private Integer prev;
103+
98104
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+
}
104123
return true;
105124
}
106125
}
107126
```
108127

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+
109198
### **JavaScript**
110199

111200
```js
@@ -121,20 +210,77 @@ class Solution {
121210
* @param {TreeNode} root
122211
* @return {boolean}
123212
*/
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) {
126217
if (!root) {
127218
return true;
128219
}
129-
if (root.val <= min || root.val >= max) {
220+
if (!dfs(root.left)) {
221+
return false;
222+
}
223+
if (prev && prev.val >= root.val) {
130224
return false;
131225
}
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;
133231
}
134-
return isValidBSTRec(root, -Infinity, Infinity);
232+
233+
return dfs(root);
135234
};
136235
```
137236

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+
138284
### **...**
139285

140286
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /