-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
47 lines (42 loc) · 1009 Bytes
/
Copy pathSolution.java
File metadata and controls
47 lines (42 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class Solution
{
private int maxCount;
private int count;
private int curr;
public int[] findMode(TreeNode root)
{
List<Integer> result = new ArrayList<>();
maxCount = 0;
count = 0;
curr = Integer.MAX_VALUE;
inOrder(result, root);
int n = result.size();
int[] res = new int[n];
for (int i = 0; i < n; i++)
{
res[i] = result.get(i);
}
return res;
}
private void inOrder(List<Integer> result, TreeNode root)
{
if (root == null)
{
return;
}
inOrder(result, root.left);
count = root.val == curr ? count + 1 : 1;
curr = root.val;
if (count > maxCount)
{
result.clear();
}
maxCount = count >= maxCount ? count : maxCount;
if (count >= maxCount)
{
result.add(curr);
}
inOrder(result, root.right);
}
}