✅✅✅[Editor's Choice]✅✅✅ Simplest solution beating the rest - Please upvote for more such easy solves
\n\n\n# Code\njava []\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        \n        List<List<Integer>> tree1 =
![Screenshot 2024-12-01 at 6.00.04\u202FPM.png](https://assets.leetcode.com/users/images/6c767ecb-5d7d-4a05-9bbd-4b36786d4126_1733056335.9960608.png)\n\n\n# Code\n```java []\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        \n        List<List<Integer>> tree1 = buildTree(edges1);\n        List<List<Integer>> tree2 = buildTree(edges2);\n        int t2Tars = getMaxTargets(tree2,k-1);\n        System.out.println(t2Tars);\n\n        int res[] = new int[edges1.length+1];\n        for(int i=0; i<res.length; i++){\n            res[i]=getSurrTars(tree1,i,k)+t2Tars;\n        }\n        return res;\n    }\n\n    private List<List<Integer>> buildTree(int[][] edges){\n        List<List<Integer>> tree = new ArrayList<>();\n        for(int i=0; i<=edges.length; i++){\n            tree.add(new ArrayList<>());\n        }\n\n        for(int[] edge : edges){\n            tree.get(edge[0]).add(edge[1]);\n            tree.get(edge[1]).add(edge[0]);\n        }\n        return tree;\n    } \n\n    private int getSurrTars(List<List<Integer>> tree, int i, int depth){\n\n        Queue<Integer> q = new LinkedList<>();\n        q.offer(i);\n        int res = 0;\n        Set<Integer> visited = new HashSet<>();\n        while(!q.isEmpty() && depth>=0){\n            int n = q.size();\n            for(int j=0; j<n; j++){\n                int p = q.poll();\n                res++;\n                visited.add(p);\n                for(int nbr : tree.get(p)){\n                    if(!visited.contains(nbr)){\n                        q.offer(nbr);\n                    }\n                }\n            }\n            depth--;\n        }\n        return res;\n    }\n\n    private int getMaxTargets(List<List<Integer>> tree, int depth){\n        int max = 0;\n        for(int i=0; i<tree.size(); i++){\n            max = Math.max(max, getSurrTars(tree,i,depth));\n        }\n        return max;\n    }\n\n\n}\n```
['Java']