-
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
package com.fishercoder.solutions.fourththousand;
import com.fishercoder.common.classes.TreeNode;
import java.util.LinkedList;
import java.util.Queue;
import java.util.TreeMap;
public class _3157 {
public static class Solution1 {
public int minimumLevel(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
TreeMap<Long, Integer> treeMap = new TreeMap<>();
int level = 1;
while (!q.isEmpty()) {
int size = q.size();
long sum = 0L;
for (int i = 0; i < size; i++) {
TreeNode curr = 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++;
}
return treeMap.firstEntry().getValue();
}
}
}