1443. Minimum Time to Collect All Apples in a Tree
https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/

from collections import defaultdict
class Solution:
def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
self.d = defaultdict(list)
for edge in edges:
f,t = edge
self.d[f].append(t)
def dfs(node):
res = 0
for child in self.d[node]:
res += dfs(child)
if node != 0 and (res > 0 or hasApple[node]):
res+=2
return res
return dfs(0)
Last updated
Was this helpful?