Java Solution using BFS (Finding Max Neighbors at a fixed distance)
Intuition\nWe can observe that adding an edge from one tree to another should be done in a way which maximises the total target nods.(i.e nodes which are reacha
# Intuition\nWe can observe that adding an edge from one tree to another should be done in a way which maximises the total target nods.(i.e nodes which are reachable from the current node within the distance k), this can only happen when we attach an edge from the current node to the node which has max neighbors at depth (k-1) in the second tree. Once we identify this max node, we can find the max neighbors in the first tree for each node and add the max neighbors from the second tree.\n\n\n# Code\n```java []\nclass Solution {\n    Map<Integer, List<Integer>> hm2;\n    Map<Integer, List<Integer>> hm1;\n    \n    public int tree1bfs(int node, int count){\n        int neighborCount = 0;\n        Deque<Integer> queue = new LinkedList<>();\n        queue.offer(node);\n        Set<Integer> visited = new HashSet<>();\n        while(!queue.isEmpty() && count >= 0){\n            int size = queue.size();\n            neighborCount += size;\n            for(int i = 0; i < size; i++){\n                int n = queue.pollFirst();\n                if(!visited.contains(n)){\n                    visited.add(n);\n                    if(hm1.containsKey(n)){\n                        List<Integer> neighbor = hm1.get(n);\n                        for(int j = 0; j < neighbor.size(); j++){\n                            if(!visited.contains(neighbor.get(j))){\n                                queue.offer(neighbor.get(j));\n                            }\n                        }\n                    }\n                }\n            }\n            count -= 1;\n        }\n\n        return neighborCount;\n    }\n    \n    public int bfs(int node, int count){\n        int neighborCount = 0;\n        Deque<Integer> queue = new LinkedList<>();\n        queue.offer(node);\n        Set<Integer> visited = new HashSet<>();\n        while(!queue.isEmpty() && count >= 0){\n            int size = queue.size();\n            neighborCount += size;\n            for(int i = 0; i < size; i++){\n                int n = queue.pollFirst();\n                if(!visited.contains(n)){\n                    visited.add(n);\n                    if(hm2.containsKey(n)){\n                        List<Integer> neighbor = hm2.get(n);\n                        for(int j = 0; j < neighbor.size(); j++){\n                            if(!visited.contains(neighbor.get(j))){\n                                queue.offer(neighbor.get(j));\n                            }\n                        }\n                    }\n                }\n            }\n            count -= 1;\n        }\n\n        return neighborCount;\n    }\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        // get the count of max nodes reachable with k-1 edges in tree 2\n        hm2 = new HashMap<>();\n        for(int i = 0; i < edges2.length; i++){\n            int from = edges2[i][0];\n            int to = edges2[i][1];\n            if(hm2.containsKey(from)){\n                List<Integer> li = hm2.get(from);\n                li.add(to);\n                hm2.put(from, li);\n            }\n            else{\n                List<Integer> li = new ArrayList<>();\n                li.add(to);\n                hm2.put(from, li);\n            }\n            if(hm2.containsKey(to)){\n                List<Integer> li = hm2.get(to);\n                li.add(from);\n                hm2.put(to, li);\n            }\n            else{\n                List<Integer> li = new ArrayList<>();\n                li.add(from);\n                hm2.put(to, li);\n            }\n        }\n\n        int max = Integer.MIN_VALUE;\n        for(int i = 0; i <= edges2.length; i++){\n            int neighborCount = bfs(i, k-1);\n            if(neighborCount > max){\n                max = neighborCount;\n            }\n        }\n\n\n        //get the count of elements which are reachable within k depth in tree 1\n\n         hm1 = new HashMap<>();\n        for(int i = 0; i < edges1.length; i++){\n            int from = edges1[i][0];\n            int to = edges1[i][1];\n            if(hm1.containsKey(from)){\n                List<Integer> li = hm1.get(from);\n                li.add(to);\n                hm1.put(from, li);\n            }\n            else{\n                List<Integer> li = new ArrayList<>();\n                li.add(to);\n                hm1.put(from, li);\n            }\n            if(hm1.containsKey(to)){\n                List<Integer> li = hm1.get(to);\n                li.add(from);\n                hm1.put(to, li);\n            }\n            else{\n                List<Integer> li = new ArrayList<>();\n                li.add(from);\n                hm1.put(to, li);\n            }\n        }\n\n        int[] ans = new int[edges1.length+1];\n        for(int i = 0; i <= edges1.length; i++){\n             ans[i] = tree1bfs(i, k) + max;\n        }\n        return ans;\n    }\n}\n```
['Breadth-First Search', 'Java']