There exist two undirected trees with n and m nodes, numbered from 0 to n - 1 and from 0 to 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 must connect one node from the first tree with another node from the second tree with an edge.
Return the minimum possible diameter of the resulting tree.
The diameter of a tree is the length of the longest path between any two nodes in the tree.
graph TD
subgraph Tree_1
0 --- 1
0 --- 2
0 --- 3
end
subgraph Tree_2
A[0] --- B[1]
end
0 -.- A
1
2
3
Input: edges1 =[[0,1],[0,2],[0,3]], edges2 =[[0,1]]Output: 3Explanation: We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.
Example 2:
graph TD
subgraph Tree_1
0 --- 1
0 --- 2
0 --- 3
2 --- 4
2 --- 5
3 --- 6
2 --- 7
end
subgraph Tree_2
A[0] --- B[1]
A --- C[2]
A --- D[3]
C --- E[4]
C --- F[5]
D --- G[6]
C --- H[7]
end
0 -.- A
1
2
3
Input: edges1 =[[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]], edges2 =[[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]]Output: 5Explanation: We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.
Constraints:
1 <= n, m <= 105
edges1.length == n - 1
edges2.length == m - 1
edges1[i].length == edges2[i].length == 2
edges1[i] = [ai, bi]
0 <= ai, bi < n
edges2[i] = [ui, vi]
0 <= ui, vi < m
The input is generated such that edges1 and edges2 represent valid trees.
To solve this problem, we need to connect one node from the first tree with another node from the second tree in such a way that the resulting tree has the minimum possible diameter. Here’s a structured approach to solving this:
Understanding Diameter:
The diameter of a tree is the length of the longest path between any two nodes. For a given tree, this can be determined using a two-pass BFS or DFS.
Combining Two Trees:
Compute the diameter (D1) of the first tree.
Compute the diameter (D2) of the second tree.
When we add an edge between any node in the first tree and any node in the second tree, the resulting diameter will at most be the maximum of:
The sum of D1 and D2 divided by 2 ((D1 + 1) / 2 + (D2 + 1) / 2 + 1).
Choosing Optimal Connection:
There’s no need to test all node pairs. The worst-case scenario would be adding one node at the diameter path in one tree to a node at the diameter path in the other tree.
⏰ Time complexity: O(n + m) for traversing both trees.
We use two-pass BFS to determine the diameter of an undirected tree. This is done by finding the furthest node from a given starting node and then finding the furthest node from that node.
Connect the trees in the way that minimally increases the diameter.
The longest possible new diameter would be considering the two initial diameters.
🧺 Space complexity: O(n + m) for storing graphs and BFS queues.