Python BFS 319ms beats 100%


  • 0
    from collections import deque
    class Solution(object):
        def killProcess(self, pid, ppid, kill):
            """
            :type pid: List[int]
            :type ppid: List[int]
            :type kill: int
            :rtype: List[int]
            """
            ans = [kill]
            ppDict = collections.defaultdict(list)
            for i, p in enumerate(ppid):
                ppDict[p].append(i)
            queue = deque([kill])
            while queue:
                for procIdx in ppDict[queue.popleft()]:
                    ans.append(pid[procIdx])
                    queue.append(pid[procIdx])
            return ans
    

Log in to reply
 

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