C++ solution with video
Video\nhttps://youtu.be/hIVslN8zDco\n\n# Code\n\nclass Solution {\npublic:\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n      
# Video\nhttps://youtu.be/hIVslN8zDco\n\n# Code\n```\nclass Solution {\npublic:\n    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n        vector<vector<int>> tree(n);\n        for (auto edge : edges) {\n            tree[edge[0]].push_back(edge[1]);\n            tree[edge[1]].push_back(edge[0]);\n        }\n\n        return max(0, DFS(0, -1, tree, hasApple) - 2);\n    }\n\n    int DFS(int node, int parent, vector<vector<int>> &tree, vector<bool> &hasApple) {\n        int time = 0;\n        for (auto child : tree[node]) {\n            if (child != parent)\n                time += DFS(child, node, tree, hasApple);\n        }\n\n        return (time > 0 || hasApple[node] ? time + 2 : 0);\n    }\n};\n```
['C++']