bfs java solution
Intuition\n1. Generate both trees using edges arrays\n2. use bfs to count no of nodes within distance k-1 in tree 2 starting at each node and take the max value
# Intuition\n1. Generate both trees using edges arrays\n2. use bfs to count no of nodes within distance k-1 in tree 2 starting at each node and take the max value.\n3. use bfs to count no of nodes within distance k in tree1 for each node and add max value from step 2\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    public void bfs(ArrayList<ArrayList<Integer>> tree,int node,int[] arr,int k){\n        int res=0;\n        Queue<Integer> q=new LinkedList<>();\n        HashSet<Integer> visited=new HashSet<>();\n        q.add(node);\n        int count=0;\n        while(!q.isEmpty() && count<=k){\n            int size=q.size();\n            for(int i=0;i<size;i++){\n            int temp=q.poll();\n            visited.add(temp);\n            for(int x:tree.get(temp)){\n                if(!visited.contains(x)){\n                    q.offer(x);\n                }\n            }\n            }\n            count++;\n        }\n        arr[node]=visited.size();\n    }\n    \n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        int n=edges1.length+1;\n        int m=edges2.length+1;\n\n        ArrayList<ArrayList<Integer>> tree1=new ArrayList<>();\n        ArrayList<ArrayList<Integer>> tree2=new ArrayList<>();\n\n        for(int i=0;i<n;i++)tree1.add(new ArrayList<>());\n        for(int i=0;i<m;i++)tree2.add(new ArrayList<>());\n\n        for(int[] e:edges1){\n            tree1.get(e[0]).add(e[1]);\n            tree1.get(e[1]).add(e[0]);\n        }\n\n        for(int[] e:edges2){\n            tree2.get(e[0]).add(e[1]);\n            tree2.get(e[1]).add(e[0]);\n        }\n\n        int[] tar1=new int[n];\n        int[] tar2=new int[m];\n\n        for(int i=0;i<m;i++){\n            bfs(tree2,i,tar2,k-1);\n        }\n        \n        int max=0;\n        for(int i:tar2){\n            max=Math.max(i,max);\n        }\n\n        for(int i=0;i<n;i++){\n            bfs(tree1,i,tar1,k);\n        }\n        \n        for(int i=0;i<n;i++){\n            tar1[i]+=max;\n       }\n        \n        return tar1;\n    }\n}\n```
['Breadth-First Search', 'Java']