BFS level order traversal
Intuition\nUse BFS to compute the number of nodes reachable within \n\uD835\uDC58\nk levels in both trees. Combine these results to find the maximum possible no
# Intuition\nUse BFS to compute the number of nodes reachable within \n\uD835\uDC58\nk levels in both trees. Combine these results to find the maximum possible nodes reachable for each node in Tree 1.\n\n# Approach\n1. Build adjacency lists for both trees.\nUse BFS to calculate the maximum reachable nodes in Tree 2 for all possible roots.\n2. For each node in Tree 1, compute the sum of its reachable nodes (using BFS) and the precomputed maximum from Tree 2.\n\n# Complexity\n- Time complexity: $$O(n* n + m * m)$$ \n\n- Space complexity: $$O(n + m)$$\n\n# Code\n```java []\nimport java.util.*;\n\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        int n = edges1.length + 1;\n        int m = edges2.length + 1;\n        List<List<Integer>> tree1 = buildAdjList(n, edges1);\n        List<List<Integer>> tree2 = buildAdjList(m, edges2);\n        int max = 0;\n        for (int i = 0; i < m; i++) {\n            max = Math.max(bfs(tree2, i, k - 1), max);\n        }\n        int ans[] = new int[n];\n        for (int i = 0; i < n; i++) {\n            ans[i] = bfs(tree1, i, k) + max;\n        }\n        return ans;\n    }\n    private int bfs(List<List<Integer>> tree, int start, int k) {\n        if (k == -1) {\n            return 0;\n        }\n\n        Queue<Integer> q = new LinkedList<>();\n        int[] vis = new int[tree.size()];\n        vis[start] = 1;\n        q.add(start);\n        int c = 1;\n\n        while (k > 0 && !q.isEmpty()) {\n            int size = q.size(); // Number of nodes at the current level\n            for (int i = 0; i < size; i++) {\n                int curr = q.poll();\n                for (int a : tree.get(curr)) {\n                    if (vis[a] == 0) {\n                        vis[a] = 1;\n                        q.add(a);\n                        c++;\n                    }\n                }\n            }\n            k--; // Decrement k after finishing the current level\n        }\n\n        return c;\n    }\n    private List<List<Integer>> buildAdjList(int nodes, int[][] edges) {\n        List<List<Integer>> adj = new ArrayList<>();\n        for (int i = 0; i < nodes; i++) {\n            adj.add(new ArrayList<>());\n        }\n        for (int[] edge : edges) {\n            adj.get(edge[0]).add(edge[1]);\n            adj.get(edge[1]).add(edge[0]);\n        }\n        return adj;\n    }\n}\n\n```
['Java']