PYTHON || EASY TO UNSERSTAND || WELL EXPLAINED
Intuition\nThe intuition behind using DFS is that we want to visit every vertex in the tree and check if it has an apple. DFS is a natural fit for this problem 
# Intuition\nThe intuition behind using DFS is that we want to visit every vertex in the tree and check if it has an apple. DFS is a natural fit for this problem because it allows us to explore all the vertices in a tree by visiting each vertex and its children in a depth-first manner.\n\nAt each vertex, we first check if any of the vertex\'s children has an apple. If any of the children has an apple, we recursively visit that child. In doing so, we keep adding the distance from the current vertex to that child to the total time spent. This way, we ensure that we only add the distance to the total time if it is necessary to visit that vertex.\n\nAdditionally, if the current vertex has an apple we also add the distance from the current vertex to its parent to the total time. This is because once we have visited all the children, we need to return back to the parent vertex and that needs to be counted in the time. Since the distance between any two vertices in the tree is always 1, we simply add 2 to the total time.\n\n# Approach\nThe approach used in this problem is a depth-first search (DFS) traversal of the tree. We start at the vertex 0 and recursively visit each vertex in the tree, keeping track of the total time spent to collect all apples.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n    def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:\n        tree = {i: [] for i in range(n)}\n        for a, b in edges:\n            tree[a].append(b)\n            tree[b].append(a)\n        def dfs(node, parent):\n            time = 0\n            for child in tree[node]:\n                if child != parent:\n                    time += dfs(child, node)\n            if (time or hasApple[node]) and node != 0:\n                time += 2\n            return time\n        return dfs(0, None)\n\n```
['Python3']