Skip to content
Veltoryx Technologies
FreeProblem 120● MediumLeetCode #690 ↗3 min readUpdated 30 October 2026

Employee Importance

Calculate the total importance value of an employee and all their direct and indirect subordinates using DFS.

01The problem

You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.

You are given an array of employees employees and an integer id that represents an employee's ID. Return the total importance value of this employee and all their direct and indirect subordinates.

InputOutputWhy
employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], id = 111Leader 1 (imp 5) + subordinate 2 (imp 3) + subordinate 3 (imp 3) = 11.

Constraints

  • 1 <= employees.length <= 2000
  • 0 <= employees[i].importance <= 100
  • All employees[i].id are unique.
  • Each employee has unique subordinates.

02How to spot it

  • "Subordinate hierarchy" / "Tree rooted at employee": The company hierarchy is a tree or forest.
  • HashMap for O(1) employee lookup: Convert the employee list into a Map<Integer, Employee> mapping id -> Employee, then run DFS from the target id.

03Pseudocode

text
byId = map id -> employee
dfs(id):
  e = byId[id]
  total = e.importance
  for sub in e.subordinates:
    total += dfs(sub)
  return total

04Watch it work

Visualizer · step through the algorithmemployees = [[1,5,[2,3]],[2,3,[]],[3,3,[]]], id = 1

subordinate tree (1 -> 2, 3)

10
21
32

status

start DFS at leader 1

Compute total importance of employee 1 and all direct/indirect subordinates.

Pseudocode

  1. byId = map id -> employee
  2. dfs(id):
  3. e = byId[id]
  4. total = e.importance
  5. for sub in e.subordinates:
  6. total += dfs(sub)
  7. return total

Variables

targetId
1
step 1 / 5

05Step-by-step dry run

We trace id = 1:

StepCurrent Employee IDImportance addedSubordinatesTotal importance so far
115[2, 3]5
223[] (leaf)5 + 3 = 8
333[] (leaf)8 + 3 = 11

Output: 11.

06Java solution

java
import java.util.*;

class Employee {
    public int id;
    public int importance;
    public List<Integer> subordinates;
    public Employee(int id, int importance, List<Integer> subordinates) {
        this.id = id;
        this.importance = importance;
        this.subordinates = subordinates;
    }
}

public class Solution {
    public static int getImportance(List<Employee> employees, int id) {
        Map<Integer, Employee> byId = new HashMap<>();
        for (Employee e : employees) {
            byId.put(e.id, e);
        }
        return dfs(byId, id);
    }

    private static int dfs(Map<Integer, Employee> byId, int id) {
        Employee e = byId.get(id);
        int total = e.importance;
        for (int subId : e.subordinates) {
            total += dfs(byId, subId);
        }
        return total;
    }

    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee(1, 5, Arrays.asList(2, 3)),
            new Employee(2, 3, Collections.emptyList()),
            new Employee(3, 3, Collections.emptyList())
        );
        System.out.println(getImportance(employees, 1));
    }
}
Expected output
11

07Why it works

The management structure is a directed acyclic tree. Mapping id -> Employee provides O(1) access to any employee record. The recursive DFS sums the root's importance and the returned importance of each subtree.

08Time and space

  • Time complexity: O(N) where N is the number of employees in the company. In the worst case, all employees are in the subtree of id.
  • Space complexity: O(N) to store the HashMap and recursion call stack.

09Edge cases

  • Employee has no subordinates: Loop doesn't execute; returns e.importance.

10Common mistakes

  • Searching the List<Employee> linearly at every recursive call: Linear search takes O(N) per lookup, making total time O(N^2). Precomputing the HashMap drops lookup to O(1).

11In the interview

  • Opening line: "I will preprocess the employee list into a HashMap for O(1) ID lookup, then perform DFS starting from the target ID to sum the importance of all subordinates in the subtree."

12Quiz

Why is building a HashMap of id -> Employee essential before starting DFS?

  • (Recommended) To achieve O(1) employee lookup during recursion instead of O(N) linear search
  • Because employees cannot be traversed without maps
  • To sort employees by importance
  • To remove duplicate IDs

Answer: To achieve O(1) employee lookup during recursion instead of O(N) linear search.

13Solve it yourself

Open LeetCode #690 and submit the solution.