Java || C++ || JavaScript || Beats 99% || DFS || Easy || Concise and Clean code
java []\nclass Solution {\n    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        \n        //creating adjacency list\n        ArrayLis
``` java []\nclass Solution {\n    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        \n        //creating adjacency list\n        ArrayList<Integer>[] a = new ArrayList[n];\n        for(int i=0;i<n;i++){\n            a[i]=new ArrayList<Integer>();\n        }\n        for(int[] x:edges){\n            a[x[0]].add(x[1]);\n            a[x[1]].add(x[0]);\n        }\n\n        return dfs(-1,0,a,hasApple);\n    }\n\n    private int dfs(int prev,int curr,ArrayList<Integer>[] a,List<Boolean> hasApple)\n    {\n        int ans=0;\n        for(int x:a[curr])\n        {\n            if(x!=prev){\n               int res = dfs(curr,x,a,hasApple);\n               if(res>0||hasApple.get(x))\n                   ans+=(res+2);\n            }\n        }\n        return ans;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n       // creating adjacency list\n        vector<vector<int>> a(n);\n        for (auto& x : edges) {\n            a[x[0]].push_back(x[1]);\n            a[x[1]].push_back(x[0]);\n        }\n\n        return dfs(-1, 0, a, hasApple);\n    }\n\n    int dfs(int prev, int curr, vector<vector<int>>& a, vector<bool>& hasApple) {\n        int ans = 0;\n        for (auto x : a[curr]) {\n            if (x != prev) {\n                int res = dfs(curr, x, a, hasApple);\n                if (res > 0 || hasApple[x]) {\n                    ans += (res + 2);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n``` javascript []\nvar minTime = function(n, edges, hasApple) {\n        // creating adjacency list\n        let a = new Array(n);\n        for (let i = 0; i < n; i++) {\n            a[i] = [];\n        }\n        for (let x of edges) {\n            a[x[0]].push(x[1]);\n            a[x[1]].push(x[0]);\n        }\n\n        return dfs(-1, 0, a, hasApple);\n    };\n\n    function dfs(prev, curr, a, hasApple) {\n        let ans = 0;\n        for (let x of a[curr]) {\n            if (x !== prev) {\n                let res = dfs(curr, x, a, hasApple);\n                if (res > 0 || hasApple[x]) {\n                    ans += (res + 2);\n                }\n            }\n        }\n        return ans;\n    }\n```
['Depth-First Search', 'C++', 'Java', 'JavaScript']