Simple python bfs solution

  • 6

    Build a (int parent: list[int] children)hashMap and do a simple bfs.

    def killProcess(self, pid, ppid, kill):
            d = collections.defaultdict(list)
            for c, p in zip(pid, ppid): d[p].append(c)
            bfs = [kill]
            for i in bfs: bfs.extend(d.get(i, []))
            return bfs

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.