Python 3   ||   9 lines,  dfs, w/ explanation   ||   T/S: 90% / 61%
Here\'s the plan:\n- We build the graphgand initialize the setseento track which nodes have been visited.\n- We start atnode = 0, as directed, and traverse the 
Here\'s the plan:\n- We build the graph`g`and initialize the set`seen`to track which nodes have been visited.\n- We start at`node = 0`, as directed, and traverse the graph recursively. We return when we hit a dead-end node, that is, a node`n`for which all nodes in g[n] already have been visited.\n- It\'s recursion; you get the idea. \n```\nclass Solution:\n    def minTime(self, n: int, edges: list[list[int]], hasApple: list[bool]) -> int:\n                          \n        seen, g = set(), defaultdict(list)\n\n        for a,b in edges: \n            g[a].append((b))  ;  g[b].append((a))\n           \n        def dfs(node: int)->int:\n            seen.add(node)    \n  \n            ans = sum(dfs(n) for n in g[node] if n not in seen) \n \n            if not ans and not hasApple[node]: return 0\n\n            return ans+2           \n\n        return max (0,dfs(0)-2)\n```\n[https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/submissions/1277744575/](https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/submissions/1277744575/)\n\nI could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(*N*), in which *N* ~ number of nodes in the tree.
['Python3']