Swift DFS
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n- see code comments. \n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- see code comments. \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```swift []\nclass Solution {\n    func maxTargetNodes(_ edges1: [[Int]], _ edges2: [[Int]], _ k: Int) -> [Int] {\n        var (adjList1, adjList2) = buildAdjLists(edges1, edges2)\n\n        // build up res by summing all target nodes for each i \n        var res = Array(repeating: 0, count: adjList1.count) \n        for i in 0...edges1.count {\n            var visited = Set<Int>()\n            visited.insert(i)\n            res[i] = dfs(adjList1, node: i, k: k, visited: &visited)\n        }\n        \n        var counter2 = [Int: Int]()\n        // for the 2nd tree, we\'re really only concerned with the max value \n        // also, we set k to k-1 since we will "use" an edge to connect tree2 to tree1  \n        var maxVal = Int.min\n        for i in 0...edges2.count {\n            var visited = Set<Int>()\n            visited.insert(i)\n            counter2[i] = dfs(adjList2, node: i, k: k - 1, visited: &visited)\n            maxVal = max(maxVal, counter2[i]!)\n        }\n        \n        return res.map { $0 + maxVal }\n    }\n\n\n\n    func dfs(_ adjList: [Int: [Int]], node: Int, k: Int, visited: inout Set<Int>) -> Int {\n        if k < 0  { return 0 }\n        if k == 0  { return 1 }\n\n        var sum = 0\n        for neighbor in adjList[node, default: []] {\n            guard !visited.contains(neighbor) else { continue }\n            visited.insert(neighbor)\n            sum += dfs(adjList, node: neighbor, k: k - 1, visited: &visited)    \n        }\n\n        return sum + 1\n    }\n\n    func buildAdjLists(_ edges1: [[Int]], _ edges2: [[Int]]) -> ([Int: [Int]], [Int: [Int]]) {\n        var adjList1 = (0...edges1.count).reduce(into: [Int: [Int]]()) { acc, v in \n            acc[v] = []\n        }\n        var adjList2 = (0...edges2.count).reduce(into: [Int: [Int]]()) { acc, v in \n            acc[v] = []\n        }\n\n        for edge in edges1 {\n            adjList1[edge[0]]!.append(edge[1])\n            adjList1[edge[1]]!.append(edge[0])\n        }\n        for edge in edges2 {\n            adjList2[edge[0]]!.append(edge[1])\n            adjList2[edge[1]]!.append(edge[0])\n        }\n        return (adjList1, adjList2)\n    }\n}\n```
['Swift']