Skip to main content
Back to problems
#690
Medium Algorithms

Employee importance

Array Hash Table Tree Depth-First Search Breadth-First Search
69.3% acceptance
Jan 13, 2026
2206
1360
You have a data structure of employee information including ID, importance value, and direct subordinates' IDs. Return the total importance value of the given employee and all their direct and indirect subordinates.

Solution

C++
Time O(n)
Space O(n)
LeetCode
solution.cpp
class Solution {
  int dfs(unordered_map<int, Employee*>& map, int id) {
    Employee* e = map[id];
    int total = e->importance;
    for (int sub : e->subordinates)
      total += dfs(map, sub);
    return total;
  }
public:
  int getImportance(vector<Employee*> employees, int id) {
    unordered_map<int, Employee*> map;
    for (Employee* e : employees) map[e->id] = e;
    return dfs(map, id);
  }
};