Java O(n*n + m*m)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nfor each tree, try to m
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nfor each tree, try to memorize the level list which memorize the num of nodes on each level, at the same time, we calculate the preSum of the node\'s numbers.\n\nfor each node in tree1, we use tree1\'s preSum of Min(k,lastLevel) + tree2\'s max preSum of Min(k-1,lastLevel)\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n*n+m*m)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n+m)$$\n# Code\n```java []\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        int n = edges1.length+1, m = edges2.length+1;\n        Map<Integer,List<Integer>> map1 = new HashMap<>();\n        for(int[] e1 : edges1){\n            map1.putIfAbsent(e1[0],new ArrayList<>());\n            map1.get(e1[0]).add(e1[1]);\n            map1.putIfAbsent(e1[1],new ArrayList<>());\n            map1.get(e1[1]).add(e1[0]);\n        }\n        Map<Integer,List<Integer>> levelMap1 = new HashMap<>();\n        for(int i=0;i<n;i++){\n            levelMap1.putIfAbsent(i,new ArrayList<>());\n            List<Integer> levelList = levelMap1.get(i);\n            dfs(i,-1,0,map1,levelList);\n            for(int j=1;j<levelList.size();j++){\n                levelList.set(j,levelList.get(j-1)+levelList.get(j));\n            }\n        }\n        Map<Integer,List<Integer>> map2 = new HashMap<>();\n        for(int[] e2 : edges2){\n            map2.putIfAbsent(e2[0],new ArrayList<>());\n            map2.get(e2[0]).add(e2[1]);\n            map2.putIfAbsent(e2[1],new ArrayList<>());\n            map2.get(e2[1]).add(e2[0]);\n        }\n        Map<Integer,List<Integer>> levelMap2 = new HashMap<>();\n        for(int i=0;i<m;i++){\n            levelMap2.putIfAbsent(i,new ArrayList<>());\n            List<Integer> levelList = levelMap2.get(i);\n            dfs(i,-1,0,map2,levelList);\n            for(int j=1;j<levelList.size();j++){\n                levelList.set(j,levelList.get(j-1)+levelList.get(j));\n            }\n        }\n        int[] ans = new int[n];\n        for(int i=0;i<n;i++){\n            List<Integer> levelList1 = levelMap1.get(i);\n            ans[i] += levelList1.get(Math.min(k,levelList1.size()-1));\n            if(k==0) continue;\n            int maxK = 0;\n            for(int j=0;j<m;j++){\n                List<Integer> levelList2 = levelMap2.get(j);\n                maxK = Math.max(maxK,levelList2.get(Math.min(k-1,levelList2.size()-1)));\n            }\n            ans[i] += maxK;\n            \n        }\n        return ans;\n    }\n    void dfs(int cur , int p, int level, Map<Integer,List<Integer>> map, List<Integer> levelList){\n        if(level >=levelList.size()){\n            levelList.add(1);\n        }else{\n            levelList.set(level,levelList.get(level)+1);\n        }\n        for(int next : map.get(cur)){\n            if(next == p) continue;\n            dfs(next,cur,level+1,map,levelList);\n        }\n    }\n}\n```
['Java']