Java HashMap<Parent, List<Child>> Solution


  • 0
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
        Map<Integer, List<Integer>> processTree = new HashMap<>();
        List<Integer> ret = new ArrayList<>();
        for(int i = 0; i<pid.size(); i++){
            if(!processTree.containsKey(ppid.get(i))){
                processTree.put(ppid.get(i),new ArrayList<Integer>());
            }
            processTree.get(ppid.get(i)).add(pid.get(i));
        }
        helper(ret, processTree, kill);
        return ret;
        
        
    }
    private void helper(List<Integer> ret, Map<Integer, List<Integer>> processTree, int kill){
        if(kill!=0){
            ret.add(kill);
        }
        if(processTree.get(kill)==null) return;
        List<Integer> tokill = processTree.get(kill);
        for(int nextkill: tokill){
            helper(ret, processTree, nextkill);
        }
    }

Log in to reply
 

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