题:
思:
广度优先搜索
码:
public int getImportance(List<Employee> employees, int id) {
// 先找到员工id
Employee start = findUseId(employees, id);
if (start == null) return 0;
int sum = 0;
if (start.subordinates == null) return start.importance;
Queue queue = new LinkedList();
sum += start.importance;
for (int i : start.subordinates) {
queue.offer(i);
}
while (!queue.isEmpty()) {
int poll = (int) queue.poll();
Employee e = findUseId(employees, poll);
sum += e.importance;
for (int i : e.subordinates) {
queue.offer(i);
}
}
return sum;
}
public static Employee findUseId(List<Employee> employees, int id) {
for (Employee e : employees) {
if (e.id == id) {
return e;
}
}
return null;
}