Simple Python Dictionary + DFS solution

  • 0
    class Solution(object):
        def killProcess(self, pid, ppid, kill):
            #create a hashmap: key is parent, value is a list of its children
            tree = { }
            for i in range(len(ppid)):
                if ppid[i] in tree:
                    tree[ppid[i]] = [pid[i]]
            #use dfs to traverse the tree branch to kill
            result = [ ]
            stack = [kill]
            while(stack != []):
                cur = stack.pop()
                if cur in tree and tree[cur] != []:
                    stack += tree[cur]
            return result

Log in to reply

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