Java | Graph | DFS | Do checkout
Do vote up if you like it :)\n\n\nclass Solution {\n    \n    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        ArrayList<Integer>[] g
**Do vote up if you like it :)**\n\n```\nclass Solution {\n    \n    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        ArrayList<Integer>[] graph = new ArrayList[n];\n        \n        for(int i = 0; i < n; i++) graph[i] = new ArrayList<>();\n        \n        for(int[] edge : edges) {\n            int u = edge[0];\n            int v = edge[1];\n            graph[u].add(v);\n            graph[v].add(u);\n        }\n        \n        boolean[] vis = new boolean[n];\n        int time = dfs(0, hasApple, vis, graph);\n        \n        // Tumhara root node v apne parent k liye +2 bhej dega, \n        // apne se aane jaane ka time, agar tumse niche se kuch time aara hoga tab\n        // Islye usko -2 kar do final answer me\n        return time > 0 ? time - 2 : time;   \n    }\n    \n    public static int dfs(int node, List<Boolean> hasApple, boolean[] vis, ArrayList<Integer>[] graph) {\n        vis[node] = true;\n        \n        int time = 0;\n        for(int child : graph[node]) {\n            if(!vis[child]) {\n                time += dfs(child, hasApple, vis, graph);\n            }\n        }\n        \n        // Niche se kuch apples aa rahe hai, usko parent tak pahuchana hai so +2 mere se aane jaane ka time  \n        // agar mere pe v apples honge tab v mko +2 time he lgega apne parent tak pahuchane me\n        if(time > 0) return time + 2; // NOTE : root node v +2 bhej dega yaha se\n        \n        else { // (time == 0) => Niche se koi apples nahi aaya ya fir tumhara koi child nahi tha\n            if(hasApple.get(node)) return 2; // Niche se koi apple nahi aaya and tumhare node pe apple hai\n            else return 0; // tumhara koi child nahi tha, leaf node will send 0\n        }\n    }\n    \n}\n```
[]