Skip to content

Commit

Permalink
Update 0501.二叉搜索树中的众数,添加C#版
Browse files Browse the repository at this point in the history
  • Loading branch information
eeee0717 committed Dec 3, 2023
1 parent bc7f72c commit 0ed1134
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions problems/0501.二叉搜索树中的众数.md
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,46 @@ pub fn find_mode(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
res
}
```
### C#
```C#
// 递归
public class Solution
{
public List<int> res = new List<int>();
public int count = 0;
public int maxCount = 0;
public TreeNode pre = null;
public int[] FindMode(TreeNode root)
{
SearchBST(root);
return res.ToArray();
}
public void SearchBST(TreeNode root)
{
if (root == null) return;
SearchBST(root.left);
if (pre == null)
count = 1;
else if (pre.val == root.val)
count++;
else
count = 1;

pre = root;
if (count == maxCount)
{
res.Add(root.val);
}
else if (count > maxCount)
{
res.Clear();
res.Add(root.val);
maxCount = count;
}
SearchBST(root.right);
}
}
```


<p align="center">
Expand Down

0 comments on commit 0ed1134

Please sign in to comment.