> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/582-kill-process.md).

# 582 Kill Process

Given **n** processes, each process has a unique **PID (process id)** and its **PPID (parent process id)**.

Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.

We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.

Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.

**Example 1:**

```
Input:

pid =  [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5

Output:
 [5,10]

Explanation:

           3
         /   \
        1     5
             /
            10
Kill 5 will also kill 10.
```

**Note:**

1. The given kill id is guaranteed to be one of the given PIDs.
2. n >= 1.

**The Idea:** Create an adjacency list that represent the tree structure of all the processes running. Then identify the process to kill, and dfs this subtree. Return the elements across this subtree.

**Complexity:** O(N) time and space where N = |PID| + |PPID|

```python
import collections


class Solution:
    def killProcess(self, pid, ppid, kill):
        """
        :type pid: List[int]
        :type ppid: List[int]
        :type kill: int
        :rtype: List[int]
        """

        g = collections.defaultdict(set)
        for ppid, pid in zip(ppid, pid):
            g[ppid].add(pid)

        killed = []
        def dfs(root):
            killed.append(root)
            for child in g[root]:
                dfs(child)

        dfs(kill)
        return killed
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/582-kill-process.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
