Skip to content

feat: add solution.cs to lc problems: No.0501 #1944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions solution/0500-0599/0501.Find Mode in Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,43 @@ func findMode(root *TreeNode) []int {
}
```

### **C#**

```cs
public class Solution {
private int mx;
private int cnt;
private TreeNode prev;
private List<int> res;

public int[] FindMode(TreeNode root) {
res = new List<int>();
Dfs(root);
int[] ans = new int[res.Count];
for (int i = 0; i < res.Count; ++i) {
ans[i] = res[i];
}
return ans;
}

private void Dfs(TreeNode root) {
if (root == null) {
return;
}
Dfs(root.left);
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
if (cnt > mx) {
res = new List<int>(new int[] { root.val });
mx = cnt;
} else if (cnt == mx) {
res.Add(root.val);
}
prev = root;
Dfs(root.right);
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,43 @@ func findMode(root *TreeNode) []int {
}
```

### **C#**

```cs
public class Solution {
private int mx;
private int cnt;
private TreeNode prev;
private List<int> res;

public int[] FindMode(TreeNode root) {
res = new List<int>();
Dfs(root);
int[] ans = new int[res.Count];
for (int i = 0; i < res.Count; ++i) {
ans[i] = res[i];
}
return ans;
}

private void Dfs(TreeNode root) {
if (root == null) {
return;
}
Dfs(root.left);
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
if (cnt > mx) {
res = new List<int>(new int[] { root.val });
mx = cnt;
} else if (cnt == mx) {
res.Add(root.val);
}
prev = root;
Dfs(root.right);
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Solution {
private int mx;
private int cnt;
private TreeNode prev;
private List<int> res;

public int[] FindMode(TreeNode root) {
res = new List<int>();
Dfs(root);
int[] ans = new int[res.Count];
for (int i = 0; i < res.Count; ++i) {
ans[i] = res[i];
}
return ans;
}

private void Dfs(TreeNode root) {
if (root == null) {
return;
}
Dfs(root.left);
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
if (cnt > mx) {
res = new List<int>(new int[] { root.val });
mx = cnt;
} else if (cnt == mx) {
res.Add(root.val);
}
prev = root;
Dfs(root.right);
}
}