mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0700.二叉搜索树,添加C#
This commit is contained in:
@ -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">
|
||||
|
Reference in New Issue
Block a user