-
Notifications
You must be signed in to change notification settings - Fork 0
/
501_Find_Mode_in_Binary_Search_Tree.java
65 lines (50 loc) · 1.32 KB
/
501_Find_Mode_in_Binary_Search_Tree.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
/*
501. Find Mode in Binary Search Tree
Difficulty: Easy
501_Find_Mode_in_Binary_Search_Tree.java
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int[] findMode(TreeNode root) {
List<Integer> ans = new ArrayList<>();
int[] count = new int[2];
inorder(root, count, ans);
return ans.stream().mapToInt(Integer::intValue).toArray();
}
private TreeNode pred = null;
private void inorder(TreeNode root, int[] count, List<Integer> ans) {
if (root == null)
return;
inorder(root.left, count, ans);
updateCount(root, count, ans);
inorder(root.right, count, ans);
}
private void updateCount(TreeNode root, int[] count, List<Integer> ans) {
if (pred != null && pred.val == root.val)
++count[0];
else
count[0] = 1;
if (count[0] > count[1]) {
count[1] = count[0];
ans.clear();
ans.add(root.val);
} else if (count[0] == count[1]) {
ans.add(root.val);
}
pred = root;
}
}