image.png
版本一:
"""
# Employee info
class Employee:
def __init__(self, id, importance, subordinates):
# It's the unique id of each node.
# unique id of this employee
self.id = id
# the importance value of this employee
self.importance = importance
# the id of direct subordinates
self.subordinates = subordinates
"""
class Solution:
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
res = 0
for item in employees:
if item.id == id:
res += item.importance
if item.subordinates == None:
return res
else:
for sub in item.subordinates:
res += self.getImportance(employees,sub)
return res
执行时间
版本二代码优化:
网友评论