C++ | DFS Solution | Easy to Understand
\nclass Solution {\npublic:\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n        \n        //Create the Graph\n        vector<
```\nclass Solution {\npublic:\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n        \n        //Create the Graph\n        vector<vector<int>> graph(n);\n        for(vector<int>& edge: edges){\n            graph[edge[0]].emplace_back(edge[1]);\n            graph[edge[1]].emplace_back(edge[0]);\n        }\n        \n        //Declare visited for storing if the nodes are visited\n        vector<bool> vis(n);\n        \n        \n        //DFS Call\n        function<int(int,int)> DFS = [&] (int node,int myCost){\n            \n            //No cost incurred if node is visited before, i.e. backtrack fron this node\n            if(vis[node])\n                return 0;\n            \n            \n            //Visit the node\n            vis[node]=true;\n           \n            \n            //Declare childCost variable for storing the cost incurred from the child\n            int childCost=0;\n            \n            //2 is added to entertain the backtrack + visit cost\n            for(int &u: graph[node])\n                childCost += DFS(u, 2);\n            \n            //If the leaveNode is not having any apple, backtrack from this node\n            if(!childCost && !hasApple[node])\n                return 0;\n            \n            \n            //In the end, return Subtree or childCost + NodeCost(if any apple found)\n            return childCost+myCost;\n        };\n        \n        //Start DFS from rootNode and initial cost equal to zero.....\n        return DFS(0,0);\n    }\n};\n```
['Depth-First Search', 'C']