C++ || DFS || O(V+E) Time & O(V+E) Space
Complexity\n- Time complexity:\nO(V + E) ->\nV is the number of nodes, ie the value if n.\nE is the number of edges.\n\n- Space complexity:\nO(V + E) ->\nV is t
# Complexity\n- Time complexity:\n$$O(V + E)$$ ->\n`V` is the number of nodes, ie the value if `n`.\n`E` is the number of `edges`.\n\n- Space complexity:\n$$O(V + E)$$ ->\n`V` is the number of nodes, ie the value if `n`.\n`E` is the number of `edges`.\n\n# Code\n```\nclass Solution {\npublic:\n    static int minTime(const int n, const vector<vector<int>>& edges, const vector<bool>& hasApple) {\n        vector<vector<int>> adj(n);\n        for (auto& e : edges) {\n            adj[e[0]].push_back(e[1]);\n            adj[e[1]].push_back(e[0]);\n        }\n\n        function<int(int, int)> dfs = [&] (int src, int parent) {\n            int ret = 0;\n            for (auto i : adj[src])\n                if (i != parent)\n                    ret += dfs(i, src);\n            return ret > 0 || hasApple[src] ? ret += 2 : 0;\n        };\n\n        return max(0, dfs(0, -1) - 2);\n    }\n};\n```
['Tree', 'Depth-First Search', 'C++']