- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1161.java
37 lines (35 loc) · 1.14 KB
/
_1161.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
packagecom.fishercoder.solutions.secondthousand;
importcom.fishercoder.common.classes.TreeNode;
importjava.util.LinkedList;
importjava.util.Queue;
importjava.util.TreeMap;
publicclass_1161 {
publicstaticclassSolution1 {
publicintmaxLevelSum(TreeNoderoot) {
if (root == null) {
return0;
}
Queue<TreeNode> q = newLinkedList<>();
q.offer(root);
TreeMap<Integer, Integer> treeMap = newTreeMap<>((a, b) -> b - a);
intlevel = 1;
while (!q.isEmpty()) {
intsize = q.size();
intsum = 0;
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);
}
}
treeMap.put(sum, level);
level++;
}
returntreeMap.firstEntry().getValue();
}
}
}