Clean BFS | JAVA
Code\njava []\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        int m = edges1.length, n = edges2.length;\n\n
# Code\n```java []\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        int m = edges1.length, n = edges2.length;\n\n        if (k == 0) {\n            int [] res = new int[m + 1];\n            Arrays.fill(res, 1);\n\n            return res;\n        }\n\n        List<List<Integer>> adj1 = new ArrayList<>();\n        for (int i=0; i<=m; i++) {\n            adj1.add(new ArrayList<>());\n        }\n\n        for (int [] edge : edges1) {\n            adj1.get(edge[0]).add(edge[1]);\n            adj1.get(edge[1]).add(edge[0]);\n        }\n\n        List<List<Integer>> adj2 = new ArrayList<>();\n        for (int i=0; i<=n; i++) {\n            adj2.add(new ArrayList<>());\n        }\n\n        for (int [] edge : edges2) {\n            adj2.get(edge[0]).add(edge[1]);\n            adj2.get(edge[1]).add(edge[0]);\n        }\n\n        int max = 1;\n        for (int i=0; i<=n; i++) {\n            max = Math.max(max, helper(adj2, i, k - 1));\n        }\n\n        int [] res = new int[m + 1];\n        for (int i=0; i<=m; i++) {\n            res[i] = max + helper(adj1, i, k);\n        }\n\n        return res;\n    }\n\n    private int helper(List<List<Integer>> adj, int u, int dist) {\n        int count = 0;\n\n        Queue<Integer> queue = new LinkedList<>();\n        Set<Integer> visited = new HashSet<>();\n        queue.offer(u);\n        visited.add(u);\n\n        int lvl = 0;\n        while (!queue.isEmpty()) {\n            if (lvl > dist) {\n                break;\n            }\n\n            int size = queue.size();\n            for (int i=0; i<size; i++) {\n                int curr = queue.poll();\n                count++;\n\n                for (int child : adj.get(curr)) {\n                    if (!visited.contains(child)) {\n                        queue.offer(child);\n                        visited.add(child);\n                    }\n                }\n            }\n            lvl++;\n        }\n\n        return count;\n    }\n}\n```
['Depth-First Search', 'Breadth-First Search', 'Java']