Java solution using recursion


  • 0
    A
    int res = 0;
        public int getImportance(List<Employee> employees, int id) {
            for(Employee employee : employees) {
                if(employee.id == id) {
                    List <Integer> list = employee.subordinates;
                    res += employee.importance;
                    for(int number : list) getImportance(employees, number);
                }       
            }
            return res;
        }
    

Log in to reply
 

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