JAVA O(N) solution, not dfs
\n    // just flag node to hasApple if sub node has apple\n    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        int p[] = new int[n];
```\n    // just flag node to hasApple if sub node has apple\n    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        int p[] = new int[n];\n        Arrays.fill(p, -1);\n        p[0] = 0;\n        for (int[] edge : edges) {\n            //direct\n            if (p[edge[1]] == -1) {\n                p[edge[1]] = edge[0];\n            } else {\n                p[edge[0]] = edge[1];\n            }\n        }\n        boolean seen[] = new boolean[n];\n        seen[0] = true;\n        int cnt = 0;\n        for (int i = 1; i < n; i++) {\n            if (hasApple.get(i)) {\n                for (int j = i; !seen[j]; j = p[j]) {\n                    seen[j] = true;\n                    cnt++;\n                }\n            }\n        }\n        return cnt * 2;\n    }\n```
[]