Java PostOrder Solution || O(n)
\n    static int time = 0;\n    \n    public static int minTime(int n, int[][] edges, List hasApple) {\n\n\t\ttime = 0;\n\t\tList[] graph = new ArrayList[n];\n\
\n    static int time = 0;\n    \n    public static int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n\n\t\ttime = 0;\n\t\tList<Integer>[] graph = new ArrayList[n];\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tgraph[i] = new ArrayList<Integer>();\n\n\t\tfor (int[] edge : edges) {\n\t\t\tgraph[edge[0]].add(edge[1]);\n\t\t\tgraph[edge[1]].add(edge[0]);\n\t\t}\n\n\t\tboolean[] visited = new boolean[n];\n\t\tminTime(n, 0, graph, hasApple, visited);\n\t\treturn (time == 0) ? 0 : time - 1;\n\t}\n\n\tpublic static boolean minTime(int n, int src, List<Integer>[] graph, List<Boolean> hasApple, boolean[] visited) {\n\n\t\tvisited[src] = true;\n\t\tList<Integer> nbrs = graph[src];\n\t\tint curr = time;\n\t\tboolean flag1 = false;\n\t\tboolean flag2 = false;\n\t\tfor (int nbr : nbrs) {\n\t\t\tif (!visited[nbr]) {\n\t\t\t\ttime++;\n\t\t\t\tflag1 = minTime(n, nbr, graph, hasApple, visited);\n\t\t\t\tif (!flag1)\n\t\t\t\t\ttime = curr;\n\t\t\t\telse if (flag1) {\n\t\t\t\t\tflag2 = true;\n\t\t\t\t\tcurr = time;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tboolean res = (hasApple.get(src) || flag2);\n\t\ttime++;\n\t\treturn res;\n\t}\n
[]