Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0098
Browse files Browse the repository at this point in the history
No.0098.Validate Binary Search Tree
  • Loading branch information
yanglbme committed Sep 10, 2022
1 parent 930dae6 commit 2c3a09c
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 11 deletions.
171 changes: 169 additions & 2 deletions solution/0000-0099/0098.Validate Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@

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

中序遍历,若是一个有效的二叉搜索树,那么遍历到的序列应该是单调递增的。所以只要比较判断遍历到的当前数是否 `>=` 上一个数即可。
**方法一:递归**

中序遍历,若是一个有效的二叉搜索树,那么遍历到的序列应该是单调递增的。所以只要比较判断遍历到的当前数是否大于上一个数即可。

或者考虑以 `root` 为根的子树,所有节点的值是否都在合法范围内,递归判断即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中节点的数量。

<!-- tabs:start -->

Expand All @@ -62,7 +68,7 @@
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def dfs(root):
nonlocal prev
if root is None:
Expand All @@ -80,6 +86,25 @@ class Solution:
return dfs(root)
```

```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def dfs(root, l, r):
if root is None:
return True
if root.val <= l or root.val >= r:
return False
return dfs(root.left, l, root.val) and dfs(root.right, root.val, r)

return dfs(root, -inf, inf)
```

### **Java**

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

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
}

private boolean dfs(TreeNode root, long l, long r) {
if (root == null) {
return true;
}
if (root.val <= l || root.val >= r) {
return false;
}
return dfs(root.left, l, root.val) && dfs(root.right, root.val, r);
}
}
```

### **C++**

```cpp
Expand Down Expand Up @@ -161,6 +219,32 @@ public:
};
```
```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
return dfs(root, LONG_MIN, LONG_MAX);
}
bool dfs(TreeNode* root, long long l, long long r) {
if (!root) return true;
if (root->val <= l || root->val >= r) return false;
return dfs(root->left, l, root->val) && dfs(root->right, root->val, r);
}
};
```

### **Go**

```go
Expand Down Expand Up @@ -197,6 +281,31 @@ func isValidBST(root *TreeNode) bool {
}
```

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidBST(root *TreeNode) bool {
return dfs(root, math.MinInt64, math.MaxInt64)
}

func dfs(root *TreeNode, l, r int64) bool {
if root == nil {
return true
}
v := int64(root.Val)
if v <= l || v >= r {
return false
}
return dfs(root.Left, l, v) && dfs(root.Right, v, r)
}
```

### **JavaScript**

```js
Expand Down Expand Up @@ -236,6 +345,33 @@ var isValidBST = function (root) {
};
```

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function (root) {
function dfs(root, l, r) {
if (!root) {
return true;
}
if (root.val <= l || root.val >= r) {
return false;
}
return dfs(root.left, l, root.val) && dfs(root.right, root.val, r);
}
return dfs(root, -Infinity, Infinity);
};
```

### **C#**

```cs
Expand Down Expand Up @@ -283,6 +419,37 @@ public class Solution {
}
```

```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public bool IsValidBST(TreeNode root) {
return dfs(root, long.MinValue, long.MaxValue);
}

public bool dfs(TreeNode root, long l, long r) {
if (root == null) {
return true;
}
if (root.val <= l || root.val >= r) {
return false;
}
return dfs(root.left, l, root.val) && dfs(root.right, root.val, r);
}
}
```

### **...**

```
Expand Down
Loading

0 comments on commit 2c3a09c

Please sign in to comment.