Python3 - DFS - Faster than 99.36%  - Easy to understand
Intuition:\nIt is difficult to simulate the process shown in the problem (such as going to child node to check if it has an apple and the go back to parent node
**Intuition:**\nIt is difficult to simulate the process shown in the problem (such as going to child node to check if it has an apple and the go back to parent node to count the edge,etc). We can think more simply that if the child node has an apple, then the edge between parent and THAT child node must be counted. So we can write a recursive function to return True if a current node has an apple or its neighbor nodes have apple(s), so that when a recursive function returns True, it means we must add 2 seconds for the edge (because we need to go back and forth, so each edge will be counted as 2 seconds)\n\n\n\n\tclass Solution:\n\t\tdef minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:\n\n\t\t\tgraph = defaultdict(list)\n\t\t\tglobalCnt = 0\n\t\t\tvisited = [False for i in range(n)]\n\n\t\t\tfor x,y in edges:\n\t\t\t\tgraph[x].append(y)\n\t\t\t\tgraph[y].append(x)\n\n\t\t\tdef dfs(root,graph,apple,visited):\n\t\t\t\tnonlocal globalCnt\n\t\t\t\tret = False\n\t\t\t\tif apple[root]:\n\t\t\t\t\tret = True\n\n\t\t\t\tvisited[root] = True\n\t\t\t\t# Mark this visited\n\t\t\t\tfor neighbor in graph[root]:\n\t\t\t\t\tif not visited[neighbor]:\n\t\t\t\t\t\tif dfs(neighbor,graph,apple,visited):\n\t\t\t\t\t\t\tglobalCnt += 2\n\t\t\t\t\t\t\tret = True\n\n\n\n\t\t\t\treturn ret\n\n\t\t\tdfs(0,graph,hasApple,visited)\n\t\t\treturn globalCnt
['Tree', 'Depth-First Search']