Problem statement

https://leetcode.com/problems/kill-process/

Solution

Just use classical dfs or bfs.

Complexity

Time complexity is O(n) and space complexity is O(h) for dfs and O(w) for bfs.

Code

class Solution:
    def killProcess(self, pid, ppid, kill):
        G, ans = defaultdict(list), []
        for x, y in zip(pid, ppid):
            G[y].append(x)

        def dfs(node):
            ans.append(node)
            for child in G[node]:
                dfs(child)

        dfs(kill)
        return ans