You have n processes forming a rooted tree structure. You are given two integer arrays pid and ppid, where pid[i] is the ID of the ith process and ppid[i] is the ID of the ith process's parent process.
Each process has only one parent process but may have multiple children processes. Only one process has ppid[i] = 0, which means this process has no parent process (the root of the tree).
When a process is killed, all of its children processes will also be killed.
Given an integer kill representing the ID of a process you want to kill, return a list of the IDs of the processes that will be killed. You may return the answer in any order.
Example 1:
Input: pid = [1,3,10,5], ppid = [3,0,5,3], kill = 5
Output: [5,10]Explanation: The processes colored in red are the processes that should be killed.
Example 2:
Input: pid = [1], ppid = [0], kill = 1
Output: [1]Constraints:
n == pid.lengthn == ppid.length1 <= n <= 5 * 10⁴1 <= pid[i] <= 5 * 10⁴0 <= ppid[i] <= 5 * 10⁴pid are unique.kill is guaranteed to be in pid.class Solution {
public List < Integer > killProcess(List < Integer > pid, List < Integer > ppid, int kill) {
List < Integer > l = new ArrayList < > ();
if (kill == 0)
return l;
l.add(kill);
for (int i = 0; i < ppid.size(); i++)
if (ppid.get(i) == kill)
l.addAll(killProcess(pid, ppid, pid.get(i)));
return l;
}
}Where is the length of the
pidandppid.
class Solution {
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));
par.children.add(map.get(pid.get(i)));
}
}
List < Integer > l = new ArrayList < > ();
l.add(kill);
getAllChildren(map.get(kill), l);
return l;
}
public void getAllChildren(Node pn, List < Integer > l) {
for (Node n: pn.children) {
l.add(n.val);
getAllChildren(n, l);
}
}
}Where is the length of the
pidandppid.
class Solution:
def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
map_dict = {}
for i in range(len(ppid)):
if ppid[i] > 0:
if ppid[i] not in map_dict:
map_dict[ppid[i]] = []
map_dict[ppid[i]].append(pid[i])
result = [kill]
self.getAllChildren(map_dict, result, kill)
return result
def getAllChildren(self, map_dict, result, kill):
if kill in map_dict:
for child_id in map_dict[kill]:
result.append(child_id)
self.getAllChildren(map_dict, result, child_id)Where is the length of the
pidandppid.
class Solution:
def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
map_dict = {}
for i in range(len(ppid)):
if ppid[i] > 0:
if ppid[i] not in map_dict:
map_dict[ppid[i]] = []
map_dict[ppid[i]].append(pid[i])
queue = deque([kill])
result = []
while queue:
r = queue.popleft()
result.append(r)
if r in map_dict:
for child_id in map_dict[r]:
queue.append(child_id)
return resultWhere is the length of the
pidandppid.