C++ || DFS
Code\n\nclass Solution {\npublic:\n    int dfs(vector<int> adj[], vector<bool> &hasApple, int node, int parent) {\n        int distance = 0; //total time to col
# Code\n```\nclass Solution {\npublic:\n    int dfs(vector<int> adj[], vector<bool> &hasApple, int node, int parent) {\n        int distance = 0; //total time to collect all apples from the root.\n        for(auto i:adj[node]) {\n            if(i!=parent) {\n                int temp=dfs(adj, hasApple, i, node); //store whether root\'s child has an apple or not.\n                if(hasApple[i]) { //if root\'s child has an apple, then we will add 2 to the time, as you have to come back.\n                    distance+=(2+temp);\n                    hasApple[node]=true; //hasApple[node] has been changed to true because its child has an apple, so for sure you will be returning back.\n                }\n            } \n        }\n        return distance;\n    }\n    \n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n        vector<int> adj[n];\n        for(int i=0;i<edges.size();i++) {\n            adj[edges[i][0]].push_back(edges[i][1]);\n            adj[edges[i][1]].push_back(edges[i][0]);\n        }\n        return dfs(adj, hasApple, 0, 0);\n    }\n};\n```
['C++']