690. 员工的重要性
方法一:深度优先搜索
class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
mp = {e.id:e for e in employees}
def dfs(id: int) -> int:
e = mp[id]
return e.importance + sum(dfs(subId) for subId in e.subordinates)
return dfs(id)
方法二:广度优先搜索
class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
mp = {e.id: e for e in employees}
total = 0
q = collections.deque([id])
while q:
curId = q.popleft()
e = mp[curId]
total += e.importance
# for subId in e.subordinates:
# q.append(subId)
q.extend(e.subordinates)
return total