Skip to main content
LeetCode 582, Medium. Topics: Tree, Depth-First Search, Breadth-First Search, Array, Hash Table. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

Problem

You have n processes forming a rooted tree structure. 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 the i^th process’s parent process. Each process has only one parent process but may have multiple children processes. Only one process has ppid[i] = 0, which means this process has no parent process (the root of the tree). When a process is killed, all of its children processes will also be killed. Given an integer kill representing the ID of a process you want to kill, return a list of the IDs of the processes that will be killed. You may return the answer in any order.

Examples

Example 1

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.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026