-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode501.java
More file actions
53 lines (48 loc) · 1.39 KB
/
leetcode501.java
File metadata and controls
53 lines (48 loc) · 1.39 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
/**
* 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 leetcode501 {
public int[] findMode(TreeNode root) {
Map<Integer,Integer> map=new HashMap<>();
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
TreeNode curr=q.poll();
map.put(curr.val,map.getOrDefault(curr.val,0)+1);
if(curr.left!=null) q.add(curr.left);
if(curr.right!=null) q.add(curr.right);
}
return result(map);
}
private int[] result(Map<Integer,Integer> map){
List<Integer> ls=new ArrayList<>();
int count=0;
for(Map.Entry<Integer,Integer> entry:map.entrySet()){
if(entry.getValue()>count){
ls.clear();
ls.add(entry.getKey());
count=entry.getValue();
}
else if(entry.getValue()==count){
ls.add(entry.getKey());
}
}
int arr[]=new int[ls.size()];
for(int i=0;i<ls.size();i++){
arr[i]=ls.get(i);
}
return arr;
}
}