There exist two undirected trees with n and m nodes, with distinct labels in ranges [0, n - 1] and [0, m - 1], respectively.
You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree. You are also given an integer k.
Node u is target to node v if the number of edges on the path from u to v is less than or equal to k. Note that a node is always target to itself.
Return an array of n integers answer, where answer[i] is the maximum possible number of nodes target to node i of the first tree if you have to connect one node from the first tree to another node in the second tree.
Note that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.
Example 1:
Input: edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]], k = 2
Output: [9,7,9,8,8]
Explanation:
i = 0, connect node 0 from the first tree to node 0 from the second tree.i = 1, connect node 1 from the first tree to node 0 from the second tree.i = 2, connect node 2 from the first tree to node 4 from the second tree.i = 3, connect node 3 from the first tree to node 4 from the second tree.i = 4, connect node 4 from the first tree to node 4 from the second tree.
Example 2:
Input: edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1
Output: [6,3,3,3,3]
Explanation:
For every i, connect node i of the first tree with any node of the second tree.

Constraints:
2 <= n, m <= 1000edges1.length == n - 1edges2.length == m - 1edges1[i].length == edges2[i].length == 2edges1[i] = [ai, bi]0 <= ai, bi < nedges2[i] = [ui, vi]0 <= ui, vi < medges1 and edges2 represent valid trees.0 <= k <= 1000The idea is to calculate the number of target nodes for each node in both trees initially using a BFS traversal, given the constraint 'k'. After obtaining these counts, for each node in the first tree, try connecting it with every node in the second tree and compute the maximum target nodes considering the nodes in the second tree.
This approach leverages BFS to efficiently compute reachability within 'k' edges by using level-order traversal.
This solution involves breaking down the existing trees into adjacency lists and calculating the reachable node counts within 'k' distance using BFS. Subsequently, it integrates both trees by evaluating each node's reachability from the first to the second tree, providing a combined reachability value for node targets.
C
Java
Time Complexity: O(N * M * K), where N is the number of nodes in the first tree, M is the number of nodes in the second tree, and K is the target distance threshold.
Space Complexity: O(N + M), considering the space for adjacency lists and auxiliary structures used during traversal.
This approach leverages DFS to find the number of target nodes reachable within 'k' from each node, utilizing memoization to store already computed results. By efficiently storing the DFS traversal results, we reduce redundant calculations, improving performance for larger graphs.
The integration of memoization with depth-first traversal provides an efficient matrix to determine targets when interlinking trees.
This solution builds trees using undirected edges and applies DFS with memoization to calculate reachable nodes efficiently within 'k'. The results are aggregated by finding the potential target nodes the first tree could exploit from the second tree.
JavaScript
Time Complexity: O(N * M), where N and M represent the number of nodes in the respective trees.
Space Complexity: O(N * K) due to memoization array usage.
| Approach | Complexity |
|---|---|
| Breadth-First Search (BFS) Based Approach | Time Complexity: O(N * M * K), where N is the number of nodes in the first tree, M is the number of nodes in the second tree, and K is the target distance threshold. Space Complexity: O(N + M), considering the space for adjacency lists and auxiliary structures used during traversal. |
| Depth-First Search (DFS) Based Approach with Memoization | Time Complexity: O(N * M), where N and M represent the number of nodes in the respective trees. |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,307 views views
Watch 9 more video solutions →Practice Maximize the Number of Target Nodes After Connecting Trees I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor