forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindModeinBinaryTree.java
64 lines (53 loc) · 1.79 KB
/
FindModeinBinaryTree.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.*;
/**
Given a binary search tree with duplicates. You have to find all the mode(s) in given binary tree.
For example:
Given binary tree [1,null,2,2],
1
\
2
/
2
return [2].
Note: If a tree has more than one mode, you can return them in any order.
*/
public class FindModeinBinaryTree {
public int[] findMode(TreeNode root) {
int[] result = new int[]{};
Map<Integer, Integer> map = new HashMap();
if (root == null) return result;
List<Integer> list = bfs(root, map);
result = new int[list.size()];
for (int i = 0; i < list.size(); i++){
result[i] = list.get(i);
}
return result;
}
private List<Integer> bfs(TreeNode root, Map<Integer, Integer> map) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
int size = queue.size();
for (int i = 0; i < size; i++){
TreeNode treeNode = queue.poll();
if (treeNode.left != null) queue.offer(treeNode.left);
if (treeNode.right != null) queue.offer(treeNode.right);
map.put(treeNode.val, map.getOrDefault(treeNode.val, 0) + 1);
}
}
int highestFrequency = 0;
List<Integer> list = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()){
if (entry.getValue() > highestFrequency){
highestFrequency = entry.getValue();
list.clear();
list.add(entry.getKey());
} else if (entry.getValue() == highestFrequency){
list.add(entry.getKey());
}
}
return list;
}
}