Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5b70551

Browse files
feat: add cs solution to lc problem: No.0501 (doocs#1944)
1 parent 095971d commit 5b70551

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

‎solution/0500-0599/0501.Find Mode in Binary Search Tree/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,43 @@ func findMode(root *TreeNode) []int {
224224
}
225225
```
226226

227+
### **C#**
228+
229+
```cs
230+
public class Solution {
231+
private int mx;
232+
private int cnt;
233+
private TreeNode prev;
234+
private List<int> res;
235+
236+
public int[] FindMode(TreeNode root) {
237+
res = new List<int>();
238+
Dfs(root);
239+
int[] ans = new int[res.Count];
240+
for (int i = 0; i < res.Count; ++i) {
241+
ans[i] = res[i];
242+
}
243+
return ans;
244+
}
245+
246+
private void Dfs(TreeNode root) {
247+
if (root == null) {
248+
return;
249+
}
250+
Dfs(root.left);
251+
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
252+
if (cnt > mx) {
253+
res = new List<int>(new int[] { root.val });
254+
mx = cnt;
255+
} else if (cnt == mx) {
256+
res.Add(root.val);
257+
}
258+
prev = root;
259+
Dfs(root.right);
260+
}
261+
}
262+
```
263+
227264
### **...**
228265

229266
```

‎solution/0500-0599/0501.Find Mode in Binary Search Tree/README_EN.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,43 @@ func findMode(root *TreeNode) []int {
211211
}
212212
```
213213

214+
### **C#**
215+
216+
```cs
217+
public class Solution {
218+
private int mx;
219+
private int cnt;
220+
private TreeNode prev;
221+
private List<int> res;
222+
223+
public int[] FindMode(TreeNode root) {
224+
res = new List<int>();
225+
Dfs(root);
226+
int[] ans = new int[res.Count];
227+
for (int i = 0; i < res.Count; ++i) {
228+
ans[i] = res[i];
229+
}
230+
return ans;
231+
}
232+
233+
private void Dfs(TreeNode root) {
234+
if (root == null) {
235+
return;
236+
}
237+
Dfs(root.left);
238+
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
239+
if (cnt > mx) {
240+
res = new List<int>(new int[] { root.val });
241+
mx = cnt;
242+
} else if (cnt == mx) {
243+
res.Add(root.val);
244+
}
245+
prev = root;
246+
Dfs(root.right);
247+
}
248+
}
249+
```
250+
214251
### **...**
215252

216253
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Solution {
2+
private int mx;
3+
private int cnt;
4+
private TreeNode prev;
5+
private List<int> res;
6+
7+
public int[] FindMode(TreeNode root) {
8+
res = new List<int>();
9+
Dfs(root);
10+
int[] ans = new int[res.Count];
11+
for (int i = 0; i < res.Count; ++i) {
12+
ans[i] = res[i];
13+
}
14+
return ans;
15+
}
16+
17+
private void Dfs(TreeNode root) {
18+
if (root == null) {
19+
return;
20+
}
21+
Dfs(root.left);
22+
cnt = prev != null && prev.val == root.val ? cnt + 1 : 1;
23+
if (cnt > mx) {
24+
res = new List<int>(new int[] { root.val });
25+
mx = cnt;
26+
} else if (cnt == mx) {
27+
res.Add(root.val);
28+
}
29+
prev = root;
30+
Dfs(root.right);
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /