problem
题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属。
solution:DFS;
/* // Employee info class Employee { public: // It's the unique ID of each node. // unique id of this employee int id; // the importance value of this employee int importance; // the id of direct subordinates vector<int> subordinates; }; */ class Solution { public: int getImportance(vector<Employee*> employees, int id) { unordered_map<int, Employee*> mymap;//err. for(auto employee:employees) mymap[employee->id] = employee; return helper(id, mymap); } int helper(int id, unordered_map<int, Employee*> m) { int res = m[id]->importance; for(auto num:m[id]->subordinates)//err. { res += helper(num, m); } return res; } };
solution:BFS;
参考
2. Grandyang;
完