-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMostFrequentElements61.java
68 lines (55 loc) · 2.05 KB
/
MostFrequentElements61.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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left = null;
right = null;
}
}
public class MostFrequentElements61 {
private Map<Integer, Integer> frequencyMap = new HashMap<>();
private int maxFrequency = 0;
// Function to perform inorder traversal and count frequencies
private void inorderTraversal(TreeNode node) {
if (node == null) return;
inorderTraversal(node.left);
// Count the frequency of the current node's value
frequencyMap.put(node.val, frequencyMap.getOrDefault(node.val, 0) + 1);
maxFrequency = Math.max(maxFrequency, frequencyMap.get(node.val));
inorderTraversal(node.right);
}
// Function to find the most frequently occurred elements
public int[] findMode(TreeNode root) {
inorderTraversal(root);
// Collect all values that have the maximum frequency
List<Integer> modes = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() == maxFrequency) {
modes.add(entry.getKey());
}
}
// Convert the list to an array
return modes.stream().mapToInt(i -> i).toArray();
}
public static void main(String[] args) {
// Constructing the BST with duplicates
TreeNode root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(2);
root.right.right = new TreeNode(3);
root.right.right.right = new TreeNode(3);
root.right.right.right.right = new TreeNode(3);
MostFrequentElements61 mfe = new MostFrequentElements61();
int[] modes = mfe.findMode(root);
System.out.print("Most frequently occurred elements: ");
for (int mode : modes) {
System.out.print(mode + " ");
}
}
}