<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <strong>vertex 0</strong> and coming back to this vertex.</em></p>

<p>The edges of the undirected tree are given in the array <code>edges</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> means that exists an edge connecting the vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>. Additionally, there is a boolean array <code>hasApple</code>, where <code>hasApple[i] = true</code> means that vertex <code>i</code> has an apple; otherwise, it does not have any apple.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/23/min_time_collect_apple_1.png" style="width: 300px; height: 212px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]
<strong>Output:</strong> 8 
<strong>Explanation:</strong> 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.  
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/23/min_time_collect_apple_2.png" style="width: 300px; height: 212px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 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.  
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>edges.length == n - 1</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub> &lt; b<sub>i</sub> &lt;= n - 1</code></li>
	<li><code>hasApple.length == n</code></li>
</ul>

class Solution:
    def minTime1(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
        
        tree =collections.defaultdict(list)
        for a, b in edges:
            tree[a].append(b)
            tree[b].append(a)

        def dfs(node, parent):
            total_time = 0
            for child in tree[node]:
                if child != parent:
                    child_time = dfs(child, node)
                    if child_time > 0 or hasApple[child]:
                        total_time += child_time + 2  # 2 seconds for edge to and from child
            return total_time
        return dfs(0, -1)

    def minTime2(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
        # Step 1: Build the adjacency list for the tree
        tree = defaultdict(list)
        for u, v in edges:
            tree[u].append(v)
            tree[v].append(u)

        # Step 2: Count the total number of apples in the tree
        total_apples = sum(hasApple)

        # If there are no apples, we don't need to traverse at all
        if total_apples == 0:
            return 0

        # Step 3: DFS function to calculate time to collect all apples
        def dfs(node, parent, collected_apples):
            nonlocal total_apples
            total_time = 0
            # Explore all children of the current node
            for child in tree[node]:
                if child != parent:  # Avoid going back to the parent
                    child_time, collected_apples = dfs(child, node, collected_apples)
                    # Collect apples from the subtree or the child node itself
                    if child_time > 0 or hasApple[child]:
                        total_time += child_time + 2  # 2 seconds for round trip (go to child and come back)
                        if hasApple[child]:
                            collected_apples += 1  # Increment the number of collected apples
                    # If all apples have been collected, stop further searching
                    if collected_apples == total_apples:
                        return total_time, collected_apples
            return total_time, collected_apples

        # Step 4: Start DFS from node 0 with 0 apples collected
        total_time, collected_apples = dfs(0, -1, 0)
        
        return total_time

    def minTime(self,n, edges, hasApple):
        # Step 1: Build the adjacency list for the tree
        tree = defaultdict(list)
        for u, v in edges:
            tree[u].append(v)
            tree[v].append(u)

        # Step 2: Find the deepest nodes containing apples using DFS
        def find_deepest_apple(node, parent):
            deepest = -1 if not hasApple[node] else 0  # Set depth to 0 if this node has an apple
            for child in tree[node]:
                if child != parent:
                    child_depth = find_deepest_apple(child, node)
                    if child_depth != -1:
                        deepest = max(deepest, child_depth + 1)
            return deepest
        
        deepest_apple_depth = find_deepest_apple(0, -1)
        
        # If no apples are present, return 0 immediately
        if deepest_apple_depth == -1:
            return 0

        # Step 3: Use DFS to calculate minimum time, but prune based on apple depth
        def dfs(node, parent, depth, max_depth):
            total_time = 0
            for child in tree[node]:
                if child != parent and depth < max_depth:  # Prune based on depth
                    child_time = dfs(child, node, depth + 1, max_depth)
                    if child_time > 0 or hasApple[child]:
                        total_time += child_time + 2
            return total_time

        # Step 4: Perform DFS up to the known max depth where apples are found
        return dfs(0, -1, 0, deepest_apple_depth)
52336