C++ || DFS ✅ || Easy Approach 🔥
\n\nIf you learn/found something new, please Upvote \uD83D\uDC4D\n\n---\n# Code\n\nclass Solution {\n    vector<bool> vis;\n    int dfs(int i, int &appleCount, 
\n```\nIf you learn/found something new, please Upvote \uD83D\uDC4D\n```\n---\n# Code\n```\nclass Solution {\n    vector<bool> vis;\n    int dfs(int i, int &appleCount, vector<vector<int>>& graph, vector<bool>& hasApple){\n        vis[i] = 1;\n\n        int time = 0;\n\n        // Traversing all adjacent node which are not visited\n        for(auto j : graph[i]){\n            int appleCount1 = 0;\n            if(vis[j] == false){\n                int time1 = dfs(j, appleCount1, graph, hasApple);\n\n                // Adding time only if traversed subtree branch has more than 0 apples\n                if(appleCount1 != 0){  \n                    time += time1;\n                    appleCount += appleCount1;\n                }\n            }\n        }\n\n        if(hasApple[i] == true)\n            count++;\n        \n        // Returning total no. of times edges traversed in this subtree + Edges traversed from parent node to this node\n        return time+2;\n    }\npublic:\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n        vis = vector<bool>(n, false);\n\n        // Building graph\n        vector<vector<int>> graph(n);\n        for(auto edge : edges){\n            graph[edge[0]].push_back(edge[1]);\n            graph[edge[1]].push_back(edge[0]);\n        }\n\n        int appleCount = 0;\n\n        // Counting extra 2 then need to decrease it\n        return dfs(0, appleCount, graph, hasApple) - 2;\n    }\n};\n```
['Depth-First Search', 'Graph', 'C++']