- dfs
class Solution {
Map<Integer, Employee> map = new HashMap<>();
public int getImportance(List<Employee> employees, int id) {
for(Employee e : employees)
map.put(e.id, e);
return dfs(id);
}
public int dfs(int id){
Employee e = map.get(id);
int total = e.importance;
for(int val : e.subordinates)
total += dfs(val);
return total;
}
}
- bfs
class Solution {
public int getImportance(List<Employee> employees, int id) {
List<Employee> list = new ArrayList<>();
Map<Integer, Employee> map = new HashMap<>();
for(Employee e : employees)
map.put(e.id, e);
list.add(map.get(id));
int start = 0, total = 0;
while(start < list.size()){
Employee e = list.get(start);
total += e.importance;
for(int val : e.subordinates)
list.add(map.get(val));
start++;
}
return total;
}
}
标签:map,total,int,list,leetcode690,重要性,Employee,员工,id
From: https://www.cnblogs.com/xzh-yyds/p/16592908.html