-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
78 lines (69 loc) · 1.89 KB
/
Solution.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ds.bst.leetcode501;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* 二叉搜索树中的众数
* LeetCode 501 https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/
*
* @author yangyi 2021年02月13日21:57:18
*/
public class Solution {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
private List<Integer> result = new LinkedList<>();
private TreeNode pre;
private int count = 0;
private int maxCount = 0;
public int[] findMode(TreeNode root) {
if (root == null) {
return new int[]{};
}
search(root);
// int[] res = new int[result.size()];
// for (int i = 0; i < result.size(); i++) {
// res[i] = result.get(i);
// }
return result.stream().mapToInt(value -> value).toArray();
}
private void search(TreeNode cur) {
if (cur == null) {
return;
}
search(cur.left);
if (pre == null) {
count = 1;
} else if (pre != null && pre.val == cur.val) {
count++;
} else {
count = 1;
}
pre = cur;
if (count == maxCount) {
result.add(cur.val);
}
if (count > maxCount) {
result.clear();
maxCount = count;
result.add(cur.val);
}
search(cur.right);
}
private TreeNode createTree() {
TreeNode node_1 = new TreeNode(1);
TreeNode node_2 = new TreeNode(2);
TreeNode node_22 = new TreeNode(2);
node_1.right = node_2;
node_2.left = node_22;
return node_1;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new Solution().findMode(new Solution().createTree())));
}
}