Leetcode Problem 1443. Minimum Time to Collect All Apples in a Tree
1443. Minimum Time to Collect All Apples in a Tree
Leetcode Solutions
Depth First Search to Collect Apples in a Tree
Create an adjacency list from the given edges to represent the tree.
Define a recursive DFS function that takes the current node and its parent as arguments.
Initialize a variable to keep track of the total time spent.
For each child of the current node, if the child is not the parent:
a. Recursively call DFS for the child.
b. If the child has an apple or the time returned by DFS is greater than 0, add the time to the total time plus 2 (for the round trip).
Return the total time spent for the current node's subtree.
Call the DFS function starting from the root node (0) with no parent (-1) and return the result.