-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution501.java
More file actions
59 lines (58 loc) · 1.74 KB
/
Solution501.java
File metadata and controls
59 lines (58 loc) · 1.74 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
public class Solution501 {
public class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
private static TreeNode prev;
private int maxCount;
private int currCount;
private static int modeCount;
private static int[] mode;
public int[] findMode(TreeNode root) {
// traverse once to get the mode count.
traverse(root);
mode = new int[modeCount];
currCount = 0;
modeCount = 0;
traverse(root);
return mode;
}
public void traverse(TreeNode root) {
if (root == null) {
return;
}
traverse(root.left);
currCount = prev.val == root.val ? currCount + 1 : 1;
if (currCount > maxCount) {
maxCount = currCount;
modeCount = 1;
} else if (currCount == maxCount) {
// won't work in the first time because the mode is null
// in the second traverse, the mode is not null
if (mode != null) {
mode[modeCount] = root.val;
}
modeCount++;
}
prev = root;
traverse(root.right);
}
public static void main(String args[]) {
Solution501 s = new Solution501();
TreeNode root = s.new TreeNode(1);
root.right = s.new TreeNode(2);
root.right.left = s.new TreeNode(2);
root.right.right = s.new TreeNode(3);
root.right.right.left = s.new TreeNode(3);
prev = root;
s.findMode(root);
System.out.println(modeCount);
for (int i = 0; i < mode.length; i++) {
System.out.println(mode[i]);
}
System.out.print(Character.getNumericValue('6') == 1);
}
}