- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3157.java
36 lines (34 loc) · 1.12 KB
/
_3157.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
packagecom.fishercoder.solutions.fourththousand;
importcom.fishercoder.common.classes.TreeNode;
importjava.util.LinkedList;
importjava.util.Queue;
importjava.util.TreeMap;
publicclass_3157 {
publicstaticclassSolution1 {
publicintminimumLevel(TreeNoderoot) {
Queue<TreeNode> q = newLinkedList<>();
q.offer(root);
TreeMap<Long, Integer> treeMap = newTreeMap<>();
intlevel = 1;
while (!q.isEmpty()) {
intsize = q.size();
longsum = 0L;
for (inti = 0; i < size; i++) {
TreeNodecurr = q.poll();
sum += curr.val;
if (curr.left != null) {
q.offer(curr.left);
}
if (curr.right != null) {
q.offer(curr.right);
}
}
if (!treeMap.containsKey(sum)) {
treeMap.put(sum, level);
}
level++;
}
returntreeMap.firstEntry().getValue();
}
}
}