3203. Find Minimum Diameter After Merging Two Trees Hard
1/**
2 * [3203] Find Minimum Diameter After Merging Two Trees
3 *
4 * 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.
5 * You must connect one node from the first tree with another node from the second tree with an edge.
6 * Return the minimum possible diameter of the resulting tree.
7 * The diameter of a tree is the length of the longest path between any two nodes in the tree.
8 *
9 * <strong class="example">Example 1:<img alt="" src="https://assets.leetcode.com/uploads/2024/04/22/example11-transformed.png" />
10 * <div class="example-block">
11 * Input: <span class="example-io">edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]</span>
12 * Output: <span class="example-io">3</span>
13 * Explanation:
14 * We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.
15 * </div>
16 * <strong class="example">Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2024/04/22/example211.png" />
18 * <div class="example-block">
19 * Input: <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>
20 * Output: <span class="example-io">5</span>
21 * Explanation:
22 * We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.
23 * </div>
24 *
25 * Constraints:
26 *
27 * 1 <= n, m <= 10^5
28 * edges1.length == n - 1
29 * edges2.length == m - 1
30 * edges1[i].length == edges2[i].length == 2
31 * edges1[i] = [ai, bi]
32 * 0 <= ai, bi < n
33 * edges2[i] = [ui, vi]
34 * 0 <= ui, vi < m
35 * The input is generated such that edges1 and edges2 represent valid trees.
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/find-minimum-diameter-after-merging-two-trees/
41// discuss: https://leetcode.com/problems/find-minimum-diameter-after-merging-two-trees/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
47 0
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_3203() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.