Java DFS Solution


  • 0
    G

    public static int getImportance(List<Employee> employees, int id) {
    Map<Integer, Employee> eMap = new HashMap<>();
    Stack<Employee> eStack = new Stack<>();
    int imp = 0;
    Employee point = new Employee();
    for(Employee employee: employees) {
    eMap.put(employee.id, employee);
    }
    point = eMap.get(id);
    while (point != null){
    imp += point.importance;
    if (point.subordinates!=null && point.subordinates.size()>0){
    for (int sid : point.subordinates){
    eStack.push(eMap.get(sid));
    }
    }
    if (!eStack.isEmpty()){
    point = eStack.pop();
    } else {
    point = null;
    }
    }
    return imp;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.