Python3 || dfs  || T/S: 98% / 94%
python3 []\nclass Solution:\n    def maxTargetNodes(self, edges1: List[List[int]], \n                       edges2: List[List[int]], k: int) -> List[int]:\n\n  
```python3 []\nclass Solution:\n    def maxTargetNodes(self, edges1: List[List[int]], \n                       edges2: List[List[int]], k: int) -> List[int]:\n\n        def dfs(node: int, level: int, par: int, count = 1) -> int:\n\n            if level < k:\n                for chd in graph[node]:\n                    if chd == par: continue\n                    count += dfs(chd, level + 1, node)\n            return count\n\n\n        n, m, mx = len(edges1) + 1, len(edges2) + 1, 0\n        if k == 0: return [1] * n\n\n        graph = [[] for _ in range(m)]\n\n        for u, v in edges2:\n            graph[u].append(v)\n            graph[v].append(u)\n\n        for node in range(m):\n            mx = max(mx, dfs(node, 1, -1))\n\n        graph = [[] for _ in range(n)]\n        for u, v in edges1:\n            graph[u].append(v)\n            graph[v].append(u)\n\n        return [dfs(node, 0, -1) + mx for node in range(n)]\n```\n\n[https://leetcode.com/problems/maximize-the-number-of-target-nodes-after-connecting-trees-i/submissions/1468842261/](https://leetcode.com/problems/maximize-the-number-of-target-nodes-after-connecting-trees-i/submissions/1468842261/)\n\nI could be wrong, but I think that time complexity is *O*(*NK* + *MK*) and space complexity is *O*(*N* + *M* + *K*), in which *N*, *M*, *K* ~ `n`, `m`, `k`.
['Python3']