diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index 184060a5..319ad1aa 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -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; +} +```

diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 56911858..f08df577 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -647,6 +647,27 @@ impl Solution { } } ``` +### C# +```C# +// 递归 +public class Solution +{ + public List res = new List(); + 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); + } +} +```

diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index bc21bab8..65e69219 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -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; +} +```