|
| 1 | +<p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code> and <code>m - 1</code>, respectively, where <code>edges1[i] = [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>edges2[i] = [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.</p> |
| 2 | + |
| 3 | +<p>You must connect one node from the first tree with another node from the second tree with an edge.</p> |
| 4 | + |
| 5 | +<p>Return the <strong>minimum </strong>possible <strong>diameter </strong>of the resulting tree.</p> |
| 6 | + |
| 7 | +<p>The <strong>diameter</strong> of a tree is the length of the <em>longest</em> path between any two nodes in the tree.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong><img alt="" src="https://assets.leetcode.com/uploads/2024/04/22/example11-transformed.png" /></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<p>We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.</p> |
| 20 | +</div> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | +<img alt="" src="https://assets.leetcode.com/uploads/2024/04/22/example211.png" /> |
| 24 | +<div class="example-block"> |
| 25 | +<p><strong>Input:</strong> <span class="example-io">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]]</span></p> |
| 26 | + |
| 27 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 28 | + |
| 29 | +<p><strong>Explanation:</strong></p> |
| 30 | + |
| 31 | +<p>We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.</p> |
| 32 | +</div> |
| 33 | + |
| 34 | +<p> </p> |
| 35 | +<p><strong>Constraints:</strong></p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li><code>1 <= n, m <= 10<sup>5</sup></code></li> |
| 39 | + <li><code>edges1.length == n - 1</code></li> |
| 40 | + <li><code>edges2.length == m - 1</code></li> |
| 41 | + <li><code>edges1[i].length == edges2[i].length == 2</code></li> |
| 42 | + <li><code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li> |
| 43 | + <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li> |
| 44 | + <li><code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li> |
| 45 | + <li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code></li> |
| 46 | + <li>The input is generated such that <code>edges1</code> and <code>edges2</code> represent valid trees.</li> |
| 47 | +</ul> |
0 commit comments