File tree 3 files changed +106
-0
lines changed
solution/0500-0599/0501.Find Mode in Binary Search Tree
3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -224,6 +224,43 @@ func findMode(root *TreeNode) []int {
224
224
}
225
225
```
226
226
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
+
227
264
### ** ...**
228
265
229
266
```
Original file line number Diff line number Diff line change @@ -211,6 +211,43 @@ func findMode(root *TreeNode) []int {
211
211
}
212
212
```
213
213
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
+
214
251
### ** ...**
215
252
216
253
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments