share my Java Solution using map and dfs!


  • 0
    T
    public class Solution {
        public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
              Map<Integer,List<Integer>> map = new HashMap<>();
              for(int i=0;i<pid.size();i++){
                  int parent_id=ppid.get(i);
                   if(!map.containsKey(parent_id)){
                       map.put(parent_id,new ArrayList<>());
                   }
                  map.get(parent_id).add(pid.get(i));
              }
            
              List<Integer> ans = new ArrayList<>();
              dfs(ans,map,kill);
              return ans;
        }
        
        public void dfs(List<Integer> res,Map<Integer,List<Integer>> map,int kill){
             List<Integer> temp = map.get(kill);
             res.add(kill);
             if(temp==null){
                 return;
             }
             for(Integer child_id:temp){
                 dfs(res,map,child_id);   
            }
        }
    }
    

Log in to reply
 

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