代码+解题思路
class Solution {
Map<Integer,Employee> map = new HashMap<>();
public int getImportance(List<Employee> employees, int id) {
int n = employees.size();
for(int i=0;i<n;i++)
map.put(employees.get(i).id,employees.get(i));
return getVal(id);
}
public int getVal(int id)
{
Employee manner = map.get(id);
int result = manner.importance;
for(int i : manner.subordinates)
{
Employee e = map.get(i);
result += e.importance;
for(int j:e.subordinates)
result += getVal(j);
}
return result;
}
}
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer,Employee> map = new HashMap<>();
int n = employees.size();
for(int i=0;i<n;i++)
map.put(employees.get(i).id, employees.get(i));
int result = 0;
Deque<Employee> d = new LinkedList<>();
d.addLast(map.get(id));
while(!d.isEmpty())
{
Employee e = d.pollFirst();
result += e.importance;
for(int i:e.subordinates)
d.addLast(map.get(i));
}
return result;
}
}