100% time and space complexity. Java Easy Solution with Explanation
Intuition\n * For tree 2 - we need to calculate the max nodes at a distance of k-1 for any node\n * For tree 1 - we need to get no of nodes at k distance from g
# Intuition\n * For tree 2 - we need to calculate the max nodes at a distance of k-1 for any node\n * For tree 1 - we need to get no of nodes at k distance from given node\n\n# Approach\n * For tree 2 - we need to calculate the max nodes at a distance of k-1 for any node\n * For tree 1 - we need to get no of nodes at k distance from given node\n\n# Complexity\n- Time complexity:\nO(n*n) - no of nodes * get max nodes\n\n- Space complexity:\nO(n2 + m2) - adjacency list of n nodes with n edges\n\n# Code\n```java []\nclass Solution {\n\n    public static int getMaxNodes (ArrayList<List<Integer>> adjList, int k, int currNode, int n, boolean[] visited) {\n        if(k == 0) {\n            return 1;\n        }\n        if(k < 1) {\n            return 0;\n        }\n        visited[currNode] = true;\n        int output = 1;\n        List<Integer> adjNodes = adjList.get(currNode);\n\n        for(int node : adjNodes) {\n            if(visited[node] != true) {\n                output += getMaxNodes(adjList, k-1, node, n, visited);\n            }\n        }\n        return output;\n    }\n\n    public static int getMaxNodes (ArrayList<List<Integer>> adjList, int k, int currNode, int n) {\n        if(k == 0) {\n            return 1;\n        }\n        if(k < 1) {\n            return 0;\n        }\n        boolean[] visited = new boolean[n];\n        visited[currNode] = true;\n        int output = 1;\n        List<Integer> adjNodes = adjList.get(currNode);\n\n        for(int node : adjNodes) {\n            output += getMaxNodes(adjList, k-1, node, n, visited);\n        }\n        return output;\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<List<Integer>> adjList1 = new ArrayList<List<Integer>>();\n        ArrayList<List<Integer>> adjList2 = new ArrayList<List<Integer>>();\n\n        for(int i = 0; i < n; i++) {\n            List<Integer> temp = new ArrayList<Integer>();\n            adjList1.add(temp);\n        }\n\n        for(int i = 0; i < m; i++) {\n            List<Integer> temp = new ArrayList<Integer>();\n            adjList2.add(temp);\n        }\n\n        for(int i = 0; i < n-1; i++) {\n            int u = edges1[i][0];\n            int v = edges1[i][1];\n            List<Integer> temp1 = adjList1.get(u);\n            temp1.add(v);\n            List<Integer> temp2 = adjList1.get(v);\n            temp2.add(u);\n\n            adjList1.set(u, temp1);\n            adjList1.set(v, temp2);\n        }\n\n        for(int i = 0; i < m-1; i++) {\n            int u = edges2[i][0];\n            int v = edges2[i][1];\n\n            List<Integer> temp1 = adjList2.get(u);\n            temp1.add(v);\n            List<Integer> temp2 = adjList2.get(v);\n            temp2.add(u);\n\n            adjList2.set(u, temp1);\n            adjList2.set(v, temp2);\n        }\n\n        int maxNodes = 0;\n        for(int i = 0; i < m; i++) {\n            int value = getMaxNodes(adjList2, k-1, i, m);\n            maxNodes = Math.max(maxNodes, value);\n        }\n\n        int[] output = new int[n];\n\n        for(int i = 0; i < n; i++) {\n            int value = getMaxNodes(adjList1, k, i, n) + maxNodes;\n            output[i] = value;\n        }\n\n        return output;\n    }\n}\n```
['Java']