Link: https://leetcode.com/problems/kill-process/
Solution:
Topics: DFS, simulation
Intuition
This is pretty clearly a tree problem. We just have to traverse every node starting at the kill
node and put all those nodes in a list. The approach is clear but the tree must be simulated because the edges are in an non-standard form.
The relationships can be refactored into an adjacency list…then the traversal and collection of killed nodes becomes trivial.
Implementation
Mnemonic
Your are in an apple orchard feeling very ADJitated. In your agitation, you cut a branch off a tree and the branch dies. You count the withered sub-branches and leaves.
Visual
Review 1
Nice little tree problem in non-standard format. The way the tree is defined is super weird, but once you realize that ppid
are parents and pid
are children, this become simple. Then its just a matter of building the graph and doing a DFS starting at the kill node. The ordering is not required so a DFS is the most optimal (ok, maybe BFS is slightly better).