https://leetcode.com/problems/employee-importance/

Source


    int getImportance(vector<Employee*> employees, int id) {
        unordered_map<int, Employee*> hashE;
        for (Employee* pEmp : employees) {
            hashE.emplace(make_pair(pEmp->id, pEmp));
        }

        return DFS(id, hashE);
    }

    int DFS(const int id, const unordered_map<int, Employee*>& hashE) {
        if (hashE.empty()) return 0;

        auto it = hashE.find(id);
        if (it == end(hashE))  return 0;

        int sumImp = 0;
        for (int subId : it->second->subordinates) {
            sumImp += DFS(subId, hashE);
        }
        return it->second->importance + sumImp;
    }

GitHub

EmployeeImportance