Skip to content

Commit b73db4e

Browse files
committed
Runtime: 8 ms (Top 17.7%) | Memory: 45.06 MB (Top 9.4%)
1 parent 015e097 commit b73db4e

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,39 @@
1+
// Runtime: 8 ms (Top 17.7%) | Memory: 45.06 MB (Top 9.4%)
2+
13
class Solution {
24
public int[] findMode(TreeNode root) {
3-
HashMap<Integer,Integer> hm = new HashMap<>();
4-
func(hm,root);
5-
Set<Integer> set = hm.keySet();
6-
ArrayList<Integer> al = new ArrayList<>(set);
7-
ArrayList<Integer> al1 = new ArrayList<>(hm.values());
8-
ArrayList<Integer> al2 = new ArrayList<>();
9-
int max = 0;
10-
for(int i = 0; i < al.size(); i++){
11-
if( al1.get(i) > max){
12-
max = al1.get(i);
5+
if(root==null) return new int[0];
6+
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); //we are taking map to count each and every value of the tree and the no of times they occurs in the tree
7+
Queue<TreeNode> qu = new LinkedList<TreeNode>(); // to itereate over the tree
8+
List<Integer> list = new ArrayList<Integer>(); //to save our result into a dynamic arraylist then will convert into static array for return it
9+
qu.add(root); // add the first root node into queue to iterate over the tree
10+
while(!qu.isEmpty()) {
11+
TreeNode tmp = qu.poll(); //we poll out the node which is last inputed to the queue
12+
if(map.containsKey(tmp.val)) { //we are checking through the map wheather the value this node have already stored into the map or not
13+
map.put(tmp.val, map.get(tmp.val)+1); //the value is already stored then we just increase the count by 1
1314
}
14-
}
15-
for(int i = 0; i < al.size(); i++){
16-
int value = al1.get(i);
17-
if(value == max){
18-
al2.add(al.get(i));
15+
else {
16+
map.put(tmp.val, 1); //if the value is unique then we store it to the map with the count 1
1917
}
18+
if(tmp.left!=null) qu.add(tmp.left); //this way we are checking wheather left node has any value or not respect to the current poped element of queue
19+
if(tmp.right!=null) qu.add(tmp.right); //the same thing of the above just this is for right node of respective poped out node
2020
}
21-
int[] arr = new int[al2.size()];
22-
int i = 0;
23-
for(Integer a : Arrays.copyOf(al2.toArray(), al2.size(), Integer[].class)){
24-
arr[i++] = a.intValue();
25-
}
26-
27-
return arr;
28-
}
29-
30-
public void func( HashMap<Integer,Integer> hm , TreeNode root) //traverse by preorder
31-
{
32-
33-
if(root != null){
34-
int value = root.val;
35-
36-
if(hm.containsKey(value)){
37-
hm.put(value,1 + hm.get(value));
38-
21+
int max = Integer.MIN_VALUE; //we are taking it because of requirement to identify highest no of repeated node available in this tree
22+
for(Integer key : map.keySet()) { //we are using keySet() for iterating over the map here key is differernt nodes and value is the no of count they have in this tree
23+
if(map.get(key)>max) { //if anything we find have greater value then previous maximum no of node like 2 2 2 - value 3, 3 3 3 3 - value 4 so now 4 is the maximum now
24+
list.clear(); //just to clear previous data that are stored into that list
25+
max = map.get(key); //now max will replaced by the new no of count of a node
26+
list.add(key); //we are adding the key which has most no of count
27+
}
28+
else if(max==map.get(key)) { //if we found another node which also has present maximum no of node count in the tree
29+
list.add(key); //we are also adding those key
30+
}
3931
}
40-
else
41-
hm.put(value,1);
42-
43-
func(hm,root.left);
44-
func(hm,root.right);
32+
//now we just create an array transfer hole data that arraylist has and then return
33+
int[] res = new int[list.size()];
34+
for(int i=0; i<list.size(); i++) {
35+
res[i] = list.get(i);
4536
}
46-
47-
37+
return res;
4838
}
49-
}
39+
}

0 commit comments

Comments
 (0)