Kill Process

Problem Id: 582 Difficulty: Medium Tag: Tree Tag: Queue


Intuition

The relationship of all the process if a tree (not a binary tree). So we should first construct a tree stored in the adjancent list format and then do a BFS to get all childrens of the given node.

Solution


from collections import defaultdict


class Solution:
    def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
        adj = defaultdict(list)
        for node, parent in zip(pid, ppid):
            adj[parent].append(node)

        children = []
        self._dfs(kill, adj, children)
        return children

    def _dfs(self, node, adj, children):
        children.append(node)
        for child in adj[node]:
            self._dfs(child, adj, children)