【每日一题】690. 员工的重要性

https://leetcode-cn.com/problems/employee-importance/

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 类似于树的层序遍历
 */
class Solution {
    public int getImportance(List<Employee> employees, int id) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(id);
        int res = 0;
        while (!queue.isEmpty()){
            int temp = queue.poll();
            Employee employee = getEmployees(employees, temp);
            if(employee != null){
                res += employee.importance;
                for(Integer n : employee.subordinates){
                    queue.add(n);
                }
            }
        }
        return res;
    }
    
    public Employee getEmployees(List<Employee> employees, int id){
        for(int i = 0; i < employees.size(); i++){
            if(employees.get(i).id == id){
                return employees.get(i);
            }
        }
        return null;
    }
}
posted @ 2021-05-02 15:44  zjy4fun  阅读(63)  评论(0编辑  收藏  举报