372. Kill Process
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 given processes form a tree structure where process 3 is the root (ppid = 0). The hierarchy is:
Process 3 has children 1 and 5.
Process 5 has child 10.
When process 5 is killed, all its descendant processes are also killed. This includes process 10. Hence, the output is 5 10.
Example 2:
Input: pid = [1], ppid = [0], kill = 1
Output: [1]
Explanation :
There is only one process (1), which is the root (ppid = 0). If we kill process 1, there are no child processes to consider. Thus, the only process that gets killed is 1.
Now Your Turn!
Pick the correct output for the given inputInput : pid = [9,3,2,7,8,1,10,5,4 ,6]Â ,ppid : [1, 2, 1 ,2 ,2 ,0 ,7 ,4 ,3 ,3Â ], kill = 10
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == pid.length
- n == ppid.length
- 1 <= n <= 5 * 104
- 1 <= pid[i] <= 5 * 104
- 0 <= ppid[i] <= 5 * 104
- Only one process has no parent.
- All the values of pid are unique.
- kill is guaranteed to be in pid.