Java Map & DFS Solution
\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        Map<Integer, List<Integer>> tree1 = new HashMap<>();\n    
```\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        Map<Integer, List<Integer>> tree1 = new HashMap<>();\n        Map<Integer, List<Integer>> tree2 = new HashMap<>();\n        constructGraph(edges1, tree1);\n        constructGraph(edges2, tree2);\n        int tree1Size = calculateTreeSize(edges1);\n        int tree2Size = calculateTreeSize(edges2);\n        int[] result = new int[tree1Size];\n        for (int i = 0; i < tree1Size; i++) {\n            Set<Integer> set = new HashSet<>();\n            set.add(i);\n            int size = findKClosest(tree1, i, k, set);\n            result[i] = size;\n        }\n        int maxValue = 0;\n        for (int i = 0; i < tree2Size; i++) {\n            Set<Integer> set = new HashSet<>();\n            set.add(i);\n            int size = findKClosest(tree2, i, k - 1, set);\n            maxValue = Math.max(maxValue, size);\n        }\n        for (int i = 0; i < tree1Size; i++) {\n            result[i] = result[i] + maxValue;\n        }\n        return result;\n    }\n    public void constructGraph(int[][] edges, Map<Integer, List<Integer>> tree) {\n        for (int i = 0; i < edges.length; i++) {\n            int[] edge = edges[i];\n            int start = edge[0];\n            int end = edge[1];\n            if (tree.containsKey(start)) {\n                List<Integer> list = tree.get(start);\n                list.add(end);\n                tree.put(start, list);\n            } else {\n                List<Integer> list = new ArrayList<>();\n                list.add(end);\n                tree.put(start, list);\n            } \n            if (tree.containsKey(end)) {\n                List<Integer> list = tree.get(end);\n                list.add(start);\n                tree.put(end, list);\n            } else {\n                List<Integer> list = new ArrayList<>();\n                list.add(start);\n                tree.put(end, list);\n            }   \n        }\n    }\n    public int calculateTreeSize(int[][] edges) {\n        int size = 0;\n        for (int i = 0; i < edges.length; i++) {\n            int[] edge = edges[i];\n            size = Math.max(size, edge[0]);\n            size = Math.max(size, edge[1]);\n        }\n        return size + 1;\n    }\n    public int findKClosest(Map<Integer, List<Integer>> map, int node, int k, Set<Integer> visited) {\n        if (!map.containsKey(node) || k < 0) {\n            return 0;\n        }\n        int sum = 1;\n        List<Integer> neighbors = map.get(node);\n        for (int i = 0; i < neighbors.size(); i++) {\n            int neighbor = neighbors.get(i);\n            if (!visited.contains(neighbor)) {\n                visited.add(neighbor);\n                sum = sum + findKClosest(map, neighbor, k - 1, visited);\n            }\n        }\n        return sum;\n    }\n}\n```
['Java']