Update 0700.二叉搜索树,添加C#

This commit is contained in:
eeee0717
2023-11-27 10:16:15 +08:00
parent 26816ec04f
commit ca89ca3097

View File

@ -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">