Simple Java DFS


  • 0
    G
    class Solution {
        public int getImportance(List<Employee> employees, int id) {
            List<Integer> list = new ArrayList<>();
            int res = 0;
            for (Employee c : employees) {
                if (c.id == id) {
                    res += c.importance;
                    list = c.subordinates;
                }
            }
            
            if (list == null) return res;
            for (int x : list) {
                res += getImportance(employees, x);
            }
            return res;
        }
    }
    

Log in to reply
 

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