Java Clean Sol
\npublic int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        List<Integer>[] lists = new List[n]; // Lists of vertices and neighbours.\n       
```\npublic int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        List<Integer>[] lists = new List[n]; // Lists of vertices and neighbours.\n        for (int i=0;i<n;i++) {\n            lists[i]=new ArrayList<>();\n        }\n        for (int[] edge : edges) {\n            lists[edge[0]].add(edge[1]);\n            lists[edge[1]].add(edge[0]);\n        }\n        boolean[] visited = new boolean[n];\n        return dfs(0,0,lists,hasApple,visited);\n    }\n\n    private int dfs(int start,int time,List<Integer>[] lists,List<Boolean> hasApple,boolean[] visited) {\n        int totalTime = time;\n        visited[start]=true;\n        for (int next : lists[start]) {\n            if(!visited[next]) {\n          // Every iteration pass totalTime+1 to visit children.\n                totalTime=dfs(next,totalTime+1,lists,hasApple,visited);\n            }\n        }\n        visited[start]=false;\n        if(hasApple.get(start) || totalTime>time) { // if current vertex has an apple or any child has an apple,\n            return start==0?totalTime:totalTime+1; //return totalTime+1 to add one to return time after visiting apple vertex.\n            // Don\'t add one for start==0 (starting vertex)\n        }\n        return start==0?totalTime:time-1;// Return time-1 since this should not be visited.\n    }\n```\n\nUpdate:\nAdded a visited array for a node can have 2 parents since its an undirected graph.\nThanks @hiepit for the Test case.\n\nInput:\n4\n[[0,2],[0,3],[1,2]]\n[false,true,false,false]\nExpected output: 4
[]