Minimum Time to Collect All Apples in a Tree Problem
Description
LeetCode Problem 1443.
Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at vertex 0 and coming back to this vertex.
The edges of the undirected tree are given in the array edges, where edges[i] = [a_i, b_i] means that exists an edge connecting the vertices a_i and b_i. Additionally, there is a boolean array hasApple, where hasApple[i] = true means that vertex i has an apple; otherwise, it does not have any apple.
Example 1:

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]
Output: 8
Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.
Example 2:

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]
Output: 6
Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.
Example 3:
Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]
Output: 0
Constraints:
1 <= n <= 10^5edges.length == n - 1edges[i].length == 20 <= a_i < b_i <= n - 1hasApple.length == n
Solution
Approach: DFS Counting Useful Edges
Every edge we walk on, we walk twice: once going down to a subtree and once coming back up, because we must end at vertex 0. So the answer is 2 times the number of edges we really need.
An edge from a vertex u down to its child v is needed exactly when the subtree of v contains at least one apple. If it has none, going down there is wasted time.
A DFS from vertex 0 can compute this bottom-up. Let dfs(u) return the time needed inside u's subtree to collect its apples and come back to u. For each child v:
- compute
t = dfs(v); - if
t > 0(there are apples deeper down) orvitself has an apple, we must go tovand back, so addt + 2; - otherwise skip
v.
The answer is dfs(0). Note the check on hasApple[v]: a child with an apple but nothing below it returns t = 0, yet we still have to visit it.
The edges are undirected and [a, b] does not tell us which end is the parent, so we build an adjacency list and pass the parent along to avoid walking back up.
Complexity: O(n) time and O(n) extra space for the adjacency list and the recursion.
class Solution:
def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
def dfs(u: int, parent: int) -> int:
# Seconds needed inside u's subtree to collect its apples and come back to u.
total = 0
for v in g[u]:
if v != parent:
t = dfs(v, u)
# Walk down to v and back up only if v's subtree has an apple.
if t > 0 or hasApple[v]:
total += t + 2
return total
return dfs(0, -1)