You have n processes forming a rooted tree. You are given two integer arrays pid and ppid, where pid[i] is the ID of the i-th process and ppid[i] is the ID of that process's parent. Each process has only one parent but may have multiple children. Only one process has ppid[i] = 0, which means it has no parent (it is the root of the tree).
When a process is killed, all of its children are also killed, along with all of their descendants.
Given an integer kill representing the ID of a process you want to kill, return the IDs of all processes that will be killed, sorted in ascending order.
Function Signature
def kill_process(pid: list[int], ppid: list[int], kill: int) -> list[int]:
Rules
-
The tree is given as two parallel arrays: process
pid[i]
has parent
ppid[i]
. A parent value of
0
marks the root, and
0
is never a process ID.
-
The result includes
kill
itself.
-
Return the killed IDs sorted in ascending order.
Constraints
-
n == pid.length
-
n == ppid.length
-
1 <= n <= 5 * 10^4
-
1 <= pid[i] <= 5 * 10^4
-
0 <= ppid[i] <= 5 * 10^4
-
Only one process has no parent.
-
All the values of
pid
are unique.
-
kill
is guaranteed to be in
pid
.
Examples
Input: pid = [1,3,10,5], ppid = [3,0,5,3], kill = 5
Output: [5,10]
Process 3 is the root with children 1 and 5, and process 10 is the only child of 5. Killing process 5 also kills its child 10, so the result is [5,10].
Input: pid = [1], ppid = [0], kill = 1
Output: [1]
There is only one process. Killing it gives [1].
Input: pid = [1,3,10,5], ppid = [3,0,5,3], kill = 3
Output: [1,3,5,10]
Process 3 is the root, so killing it kills every process in the tree. Sorted in ascending order, that is [1,3,5,10].