JAVA | BFS | Clean Implementation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\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```java []\nclass Solution {\n    HashMap<Integer, List<Integer>> graph1 = new HashMap<>();\n    HashMap<Integer, List<Integer>> graph2 = new HashMap<>();\n    \n    HashMap<Integer, Integer> map = new HashMap<>();\n    int maxNode = -1;\n\n    public int bfs(int index, int k, HashMap<Integer, List<Integer>> graph){\n        Queue<Integer> q = new LinkedList<>();\n        HashSet<Integer> seen = new HashSet<>();\n        q.add(index);\n        seen.add(index);\n\n        int level = 0, count=1;\n        while(!q.isEmpty()){\n            int size = q.size();\n            for(int i=0; i<size; i++){\n                int node = q.poll();\n                for(Integer neighbour: graph.get(node)){\n                    if(!seen.contains(neighbour) && level < k){\n                        q.add(neighbour);\n                        seen.add(neighbour);\n                        count++;\n                    }\n                }\n            }\n            level++;\n        }\n\n        return count;\n    }\n\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        int n = edges1.length, m=edges2.length;\n\n        for(int[] row: edges1){\n            graph1.computeIfAbsent(row[0], x->new ArrayList<Integer>()).add(row[1]);\n            graph1.computeIfAbsent(row[1], x->new ArrayList<Integer>()).add(row[0]);\n        }\n        for(int[] row: edges2){\n            graph2.computeIfAbsent(row[0], x->new ArrayList<Integer>()).add(row[1]);\n            graph2.computeIfAbsent(row[1], x->new ArrayList<Integer>()).add(row[0]);\n        }\n\n        for(int i=0; i<=n; i++){\n            // count total nodes at K distance from ith node\n            int totalNodes = bfs(i, k, graph1);\n            map.put(i, totalNodes);\n        }\n        for(int i=0; i<=m; i++){\n            // count total nodes at K-1 distance from ith node\n            int totalNodes = bfs(i, k-1, graph2);\n            maxNode = Math.max(maxNode, totalNodes);\n        }\n\n        int[] result = new int[n+1];\n        for(int i=0; i<=n; i++){\n            result[i] += map.get(i);\n            if(k > 0){\n                result[i] += maxNode;\n            }\n        }\n        return result;\n    }\n}\n```
['Breadth-First Search', 'Java']