. - 力扣(LeetCode) 直接递归就行 """ # Definition for Employee. class Employee: def __init__(self, id: int, importance: int, subordinates: List[int]): self.id = id self.importance = importance self.subordinates = subordinates """ from typing import List class Solution: def getImportance(self, employees: List['Employee'], id: int) -> int: def getAllEmployees(eid, score=0): emp = [e for e in employees if e.id == eid][0] tmp_score = score + emp.importance for sub in emp.subordinates: tmp_score = getAllEmployees(sub, tmp_score) return tmp_score all_sc = getAllEmployees(id) return all_sc