Java 18-line Solution


  • 0
    D
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
    		Map<Integer, List<Integer>> map = new HashMap<>();
    		for (int i = 0; i < ppid.size(); i++) {
    			map.computeIfAbsent(ppid.get(i), k -> new ArrayList<Integer>()).add(pid.get(i));
    		}
    		List<Integer> ans = new ArrayList<>();
    		Queue<Integer> queue = new LinkedList<>();
    		queue.offer(kill);
    		while (!queue.isEmpty()) {
    			int target = queue.poll();
    			ans.add(target);
    			if (map.containsKey(target)) {
    				List<Integer> tmp = map.get(target);
    				for (int num : tmp) {
    					queue.offer(num);
    				}
    			}
    		}
    		return ans;
    	}
    

Log in to reply
 

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