# 582. Kill Process

{% embed url="<https://leetcode.com/problems/kill-process/>" %}

题目其实给了hint，说类似`tree`结构，那其实就是从给出的`kill`开始往下做 traversal，唯一需要解决的问题是 parent/children 关系的查询，直接先`hash map`一下`pid/ppid`解决，然后`DFS`。

```python
class Solution:
    def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
        children = collections.defaultdict(list)
        for i, p in enumerate(ppid):
            children[p].append(pid[i])
            
        res, stack = [], [kill]
        while stack:
            p = stack.pop()
            res.append(p)
            if p in children:
                stack.extend(children[p])
        
        return res
```
