Class Solution
-
- All Implemented Interfaces:
public final class Solution3203 - Find Minimum Diameter After Merging Two Trees\.
Hard
There exist two undirected trees with
nandmnodes, numbered from0ton - 1and from0tom - 1, respectively. You are given two 2D integer arraysedges1andedges2of lengthsn - 1andm - 1, respectively, where <code>edges1i = a<sub>i</sub>, b<sub>i</sub></code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the first tree and <code>edges2i = u<sub>i</sub>, v<sub>i</sub></code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> 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.
Example 1:
Input: edges1 = \[\[0,1],0,2,0,3], edges2 = \[\[0,1]]
Output: 3
Explanation:
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:
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: 5
Explanation:
We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.
Constraints:
<code>1 <= n, m <= 10<sup>5</sup></code>
edges1.length == n - 1edges2.length == m - 1edges1[i].length == edges2[i].length == 2<code>edges1i = a<sub>i</sub>, b<sub>i</sub></code>
<code>0 <= a<sub>i</sub>, b<sub>i</sub>< n</code>
<code>edges2i = u<sub>i</sub>, v<sub>i</sub></code>
<code>0 <= u<sub>i</sub>, v<sub>i</sub>< m</code>
The input is generated such that
edges1andedges2represent valid trees.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-