##### Welcome to Subscribe On Youtube
• import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**

Given n processes, each process has a unique PID (process id) and its PPID (parent process id).

Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.

We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.

Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.

Example 1:
Input:
pid =  [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
Output: [5,10]
Explanation:
3
/   \
1     5
/
10
Kill 5 will also kill 10.
Note:
The given kill id is guaranteed to be one of the given PIDs.
n >= 1.

*/

public class Kill_Process {

public class Solution {

public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
List< Integer > result = new ArrayList< >();
if (kill == 0) {
return result;
}

for (int i = 0; i < ppid.size(); i++)
if (ppid.get(i) == kill)
return result;
}
}

public class Solution_Tree {
class Node {
int val;
List < Node > children = new ArrayList < > ();
}
public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
HashMap< Integer, Node > map = new HashMap < > ();
for (int id: pid) {
Node node = new Node();
node.val = id;
map.put(id, node);
}
for (int i = 0; i < ppid.size(); i++) {
if (ppid.get(i) > 0) {
Node par = map.get(ppid.get(i));
}
}
List < Integer > result = new ArrayList < > ();
getAllChildren(map.get(kill), result);
return result;
}
public void getAllChildren(Node pn, List < Integer > l) {
for (Node n: pn.children) {
getAllChildren(n, l);
}
}
}

public class Solution_dfsWithCache {
public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
HashMap < Integer, List < Integer >> map = new HashMap < > ();
for (int i = 0; i < ppid.size(); i++) {
if (ppid.get(i) > 0) {
List < Integer > l = map.getOrDefault(ppid.get(i), new ArrayList < Integer > ());
map.put(ppid.get(i), l);
}
}
List < Integer > result = new ArrayList < > ();
getAllChildren(map, result, kill);
return result;
}
public void getAllChildren(HashMap < Integer, List < Integer >> map, List < Integer > l, int kill) {
if (map.containsKey(kill))
for (int id: map.get(kill)) {
getAllChildren(map, l, id);
}
}
}

}

############

class Solution {
private Map<Integer, List<Integer>> g;
private List<Integer> ans;

public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
g = new HashMap<>();
for (int i = 0, n = pid.size(); i < n; ++i) {
int c = pid.get(i), p = ppid.get(i);
}
ans = new ArrayList<>();
dfs(kill);
return ans;
}

private void dfs(int u) {
for (int v : g.getOrDefault(u, new ArrayList<>())) {
dfs(v);
}
}
}

• // OJ: https://leetcode.com/problems/kill-process
// Time: O(N)
// Space: O(N)
class Solution {
private:
unordered_map<int, vector<int>> m;
vector<int> ans;
void dfs(int kill) {
ans.push_back(kill);
for (int c : m[kill]) dfs(c);
}
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
for (int i = 0; i < pid.size(); ++i) m[ppid[i]].push_back(pid[i]);
dfs(kill);
return ans;
}
};

• class Solution:
def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
def dfs(u):
ans.append(u)
for v in g[u]:
dfs(v)

g = defaultdict(list)
n = len(pid)
for c, p in zip(pid, ppid):
g[p].append(c)
ans = []
dfs(kill)
return ans

############

class Solution(object):
def killProcess(self, pid, ppid, kill):
"""
:type pid: List[int]
:type ppid: List[int]
:type kill: int
:rtype: List[int]
"""
n = len(pid)

mpppid = {}

for i in range(n):
if mpppid.has_key(ppid[i]):
mpppid[ppid[i]].append(i)
else:
mpppid[ppid[i]] = [i]
res = [kill]

def dfs(x):
if not mpppid.has_key(x):
return
for i in mpppid[x]:
y = pid[i]
res.append(y)
dfs(y)

dfs(kill)
return res


• func killProcess(pid []int, ppid []int, kill int) []int {
g := make(map[int][]int)
for i, c := range pid {
p := ppid[i]
g[p] = append(g[p], c)
}
var ans []int
var dfs func(u int)
dfs = func(u int) {
ans = append(ans, u)
for _, v := range g[u] {
dfs(v)
}
}
dfs(kill)
return ans
}

• class Solution {
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
Map<Integer, List<Integer>> parentChildrenMap = new HashMap<Integer, List<Integer>>();
int size = pid.size();
for (int i = 0; i < size; i++) {
int parentId = ppid.get(i), childId = pid.get(i);
List<Integer> children = parentChildrenMap.getOrDefault(parentId, new ArrayList<Integer>());
parentChildrenMap.put(parentId, children);
}
List<Integer> killedPid = new ArrayList<Integer>();
queue.offer(kill);
while (!queue.isEmpty()) {
int curId = queue.poll();
List<Integer> children = parentChildrenMap.getOrDefault(curId, new ArrayList<Integer>());
for (int child : children)
queue.offer(child);
}
return killedPid;
}
}

############

class Solution {
private Map<Integer, List<Integer>> g;
private List<Integer> ans;

public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
g = new HashMap<>();
for (int i = 0, n = pid.size(); i < n; ++i) {
int c = pid.get(i), p = ppid.get(i);
}
ans = new ArrayList<>();
dfs(kill);
return ans;
}

private void dfs(int u) {
for (int v : g.getOrDefault(u, new ArrayList<>())) {
dfs(v);
}
}
}

• // OJ: https://leetcode.com/problems/kill-process
// Time: O(N)
// Space: O(N)
class Solution {
private:
unordered_map<int, vector<int>> m;
vector<int> ans;
void dfs(int kill) {
ans.push_back(kill);
for (int c : m[kill]) dfs(c);
}
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
for (int i = 0; i < pid.size(); ++i) m[ppid[i]].push_back(pid[i]);
dfs(kill);
return ans;
}
};

• class Solution:
def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
def dfs(u):
ans.append(u)
for v in g[u]:
dfs(v)

g = defaultdict(list)
n = len(pid)
for c, p in zip(pid, ppid):
g[p].append(c)
ans = []
dfs(kill)
return ans

############

class Solution(object):
def killProcess(self, pid, ppid, kill):
"""
:type pid: List[int]
:type ppid: List[int]
:type kill: int
:rtype: List[int]
"""
n = len(pid)

mpppid = {}

for i in range(n):
if mpppid.has_key(ppid[i]):
mpppid[ppid[i]].append(i)
else:
mpppid[ppid[i]] = [i]
res = [kill]

def dfs(x):
if not mpppid.has_key(x):
return
for i in mpppid[x]:
y = pid[i]
res.append(y)
dfs(y)

dfs(kill)
return res


• func killProcess(pid []int, ppid []int, kill int) []int {
g := make(map[int][]int)
for i, c := range pid {
p := ppid[i]
g[p] = append(g[p], c)
}
var ans []int
var dfs func(u int)
dfs = func(u int) {
ans = append(ans, u)
for _, v := range g[u] {
dfs(v)
}
}
dfs(kill)
return ans
}