Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2349 from eeee0717/master
Browse files Browse the repository at this point in the history
Update 0700.二叉搜索树,添加C#
  • Loading branch information
youngyangyang04 committed Dec 1, 2023
2 parents 9d4c263 + bc7f72c commit 52eac4c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions problems/0098.验证二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,20 @@ impl Solution {
}
}
```
### C#
```C#
// 递归
public long val = Int64.MinValue;
public bool IsValidBST(TreeNode root)
{
if (root == null) return true;
bool left = IsValidBST(root.left);
if (root.val > val) val = root.val;
else return false;
bool right = IsValidBST(root.right);
return left && right;
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
21 changes: 21 additions & 0 deletions problems/0530.二叉搜索树的最小绝对差.md
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,27 @@ impl Solution {
}
}
```
### C#
```C#
// 递归
public class Solution
{
public List<int> res = new List<int>();
public int GetMinimumDifference(TreeNode root)
{
Traversal(root);
return res.SelectMany((x, i) => res.Skip(i + 1).Select(y => Math.Abs(x - y))).Min();

}
public void Traversal(TreeNode root)
{
if (root == null) return;
Traversal(root.left);
res.Add(root.val);
Traversal(root.right);
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
22 changes: 22 additions & 0 deletions problems/0700.二叉搜索树中的搜索.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,28 @@ impl Solution {
}
}
```
### C#
```C#
// 递归
public TreeNode SearchBST(TreeNode root, int val)
{
if (root == null || root.val == val) return root;
if (root.val > val) return SearchBST(root.left, val);
if (root.val < val) return SearchBST(root.right, val);
return null;
}
// 迭代
public TreeNode SearchBST(TreeNode root, int val)
{
while (root != null)
{
if (root.val > val) root = root.left;
else if (root.val < val) root = root.right;
else return root;
}
return null;
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down

0 comments on commit 52eac4c

Please sign in to comment.