Python simple DFS


  • 0
    D
    class Solution(object):
        def killProcess(self, pid, ppid, kill):
            if not pid or not ppid:
                return []
            adj = collections.defaultdict(list)
            for parent, child in zip(ppid, pid):
                adj[parent].append(child)
            ans = []
            self.dfs(kill, adj, ans)
            return ans
        
        def dfs(self, curr, adj, ans):
            ans.append(curr)
            for child in adj[curr]:
                self.dfs(child, adj, ans)
    

Log in to reply
 

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