[C++] DFS, Time: O(N), Space: O(N)
Approach\n Describe your approach to solving the problem. \n1. create graph\n2. dfs: whether there exists red node below cur node\n\n# Complexity\n- Time comple
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. create graph\n2. dfs: whether there exists red node below cur node\n\n# Complexity\n- Time complexity: $$O(N)$$, for all nodes\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$, for creating graph with n-1 edges\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution\n{\nprivate:\n    vector<vector<int>> graph;\n    int result;\n    bool dfs(const int previous, const int current, const vector<bool> &hasApple)\n    { // whether there exists red node below cur\n        bool red = false;\n        for (const int next : graph[current])\n            if (next != previous && dfs(current, next, hasApple))\n            {\n                result += 2; // go down and go back\n                red = true;\n            }\n        return red || hasApple[current]; // red node below or current is red node\n    }\n\npublic:\n    int minTime(int n, vector<vector<int>> &edges, vector<bool> &hasApple)\n    {\n        graph.resize(n, vector<int>{});\n        for (const vector<int> &edge : edges)\n        {\n            graph[edge[0]].emplace_back(edge[1]);\n            graph[edge[1]].emplace_back(edge[0]);\n        }\n        result = 0;\n        dfs(-1, 0, hasApple);\n        return result;\n    }\n};\n```
['Depth-First Search', 'Recursion', 'C++']