Skip to content

Commit 4db3791

Browse files
authored
feat: add solutions to lc problem: No.3203 (#4572)
No.3203.Find Minimum Diameter After Merging Two Trees
1 parent 47cb088 commit 4db3791

File tree

5 files changed

+248
-3
lines changed

5 files changed

+248
-3
lines changed

solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public:
211211
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
212212
d1 := treeDiameter(edges1)
213213
d2 := treeDiameter(edges2)
214-
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
214+
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
215215
}
216216

217217
func treeDiameter(edges [][]int) (ans int) {
@@ -275,6 +275,91 @@ function treeDiameter(edges: number[][]): number {
275275
}
276276
```
277277

278+
#### Rust
279+
280+
```rust
281+
impl Solution {
282+
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
283+
let d1 = Self::tree_diameter(&edges1);
284+
let d2 = Self::tree_diameter(&edges2);
285+
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
286+
}
287+
288+
fn tree_diameter(edges: &Vec<Vec<i32>>) -> i32 {
289+
let n = edges.len() + 1;
290+
let mut g = vec![vec![]; n];
291+
for e in edges {
292+
let a = e[0] as usize;
293+
let b = e[1] as usize;
294+
g[a].push(b);
295+
g[b].push(a);
296+
}
297+
let mut ans = 0;
298+
let mut a = 0;
299+
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
300+
for &j in &g[i] {
301+
if j as isize != fa {
302+
dfs(g, j, i as isize, t + 1, ans, a);
303+
}
304+
}
305+
if *ans < t {
306+
*ans = t;
307+
*a = i;
308+
}
309+
}
310+
dfs(&g, 0, -1, 0, &mut ans, &mut a);
311+
dfs(&g, a, -1, 0, &mut ans, &mut a);
312+
ans
313+
}
314+
}
315+
```
316+
317+
#### C#
318+
319+
```cs
320+
public class Solution {
321+
private List<int>[] g;
322+
private int ans;
323+
private int a;
324+
325+
public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
326+
int d1 = TreeDiameter(edges1);
327+
int d2 = TreeDiameter(edges2);
328+
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
329+
}
330+
331+
public int TreeDiameter(int[][] edges) {
332+
int n = edges.Length + 1;
333+
g = new List<int>[n];
334+
for (int k = 0; k < n; ++k) {
335+
g[k] = new List<int>();
336+
}
337+
ans = 0;
338+
a = 0;
339+
foreach (var e in edges) {
340+
int a = e[0], b = e[1];
341+
g[a].Add(b);
342+
g[b].Add(a);
343+
}
344+
Dfs(0, -1, 0);
345+
Dfs(a, -1, 0);
346+
return ans;
347+
}
348+
349+
private void Dfs(int i, int fa, int t) {
350+
foreach (int j in g[i]) {
351+
if (j != fa) {
352+
Dfs(j, i, t + 1);
353+
}
354+
}
355+
if (ans < t) {
356+
ans = t;
357+
a = i;
358+
}
359+
}
360+
}
361+
```
362+
278363
<!-- tabs:end -->
279364

280365
<!-- solution:end -->

solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README_EN.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public:
209209
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
210210
d1 := treeDiameter(edges1)
211211
d2 := treeDiameter(edges2)
212-
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
212+
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
213213
}
214214

215215
func treeDiameter(edges [][]int) (ans int) {
@@ -273,6 +273,91 @@ function treeDiameter(edges: number[][]): number {
273273
}
274274
```
275275

276+
#### Rust
277+
278+
```rust
279+
impl Solution {
280+
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
281+
let d1 = Self::tree_diameter(&edges1);
282+
let d2 = Self::tree_diameter(&edges2);
283+
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
284+
}
285+
286+
fn tree_diameter(edges: &Vec<Vec<i32>>) -> i32 {
287+
let n = edges.len() + 1;
288+
let mut g = vec![vec![]; n];
289+
for e in edges {
290+
let a = e[0] as usize;
291+
let b = e[1] as usize;
292+
g[a].push(b);
293+
g[b].push(a);
294+
}
295+
let mut ans = 0;
296+
let mut a = 0;
297+
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
298+
for &j in &g[i] {
299+
if j as isize != fa {
300+
dfs(g, j, i as isize, t + 1, ans, a);
301+
}
302+
}
303+
if *ans < t {
304+
*ans = t;
305+
*a = i;
306+
}
307+
}
308+
dfs(&g, 0, -1, 0, &mut ans, &mut a);
309+
dfs(&g, a, -1, 0, &mut ans, &mut a);
310+
ans
311+
}
312+
}
313+
```
314+
315+
#### C#
316+
317+
```cs
318+
public class Solution {
319+
private List<int>[] g;
320+
private int ans;
321+
private int a;
322+
323+
public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
324+
int d1 = TreeDiameter(edges1);
325+
int d2 = TreeDiameter(edges2);
326+
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
327+
}
328+
329+
public int TreeDiameter(int[][] edges) {
330+
int n = edges.Length + 1;
331+
g = new List<int>[n];
332+
for (int k = 0; k < n; ++k) {
333+
g[k] = new List<int>();
334+
}
335+
ans = 0;
336+
a = 0;
337+
foreach (var e in edges) {
338+
int a = e[0], b = e[1];
339+
g[a].Add(b);
340+
g[b].Add(a);
341+
}
342+
Dfs(0, -1, 0);
343+
Dfs(a, -1, 0);
344+
return ans;
345+
}
346+
347+
private void Dfs(int i, int fa, int t) {
348+
foreach (int j in g[i]) {
349+
if (j != fa) {
350+
Dfs(j, i, t + 1);
351+
}
352+
}
353+
if (ans < t) {
354+
ans = t;
355+
a = i;
356+
}
357+
}
358+
}
359+
```
360+
276361
<!-- tabs:end -->
277362

278363
<!-- solution:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class Solution {
2+
private List<int>[] g;
3+
private int ans;
4+
private int a;
5+
6+
public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
7+
int d1 = TreeDiameter(edges1);
8+
int d2 = TreeDiameter(edges2);
9+
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
10+
}
11+
12+
public int TreeDiameter(int[][] edges) {
13+
int n = edges.Length + 1;
14+
g = new List<int>[n];
15+
for (int k = 0; k < n; ++k) {
16+
g[k] = new List<int>();
17+
}
18+
ans = 0;
19+
a = 0;
20+
foreach (var e in edges) {
21+
int a = e[0], b = e[1];
22+
g[a].Add(b);
23+
g[b].Add(a);
24+
}
25+
Dfs(0, -1, 0);
26+
Dfs(a, -1, 0);
27+
return ans;
28+
}
29+
30+
private void Dfs(int i, int fa, int t) {
31+
foreach (int j in g[i]) {
32+
if (j != fa) {
33+
Dfs(j, i, t + 1);
34+
}
35+
}
36+
if (ans < t) {
37+
ans = t;
38+
a = i;
39+
}
40+
}
41+
}

solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
22
d1 := treeDiameter(edges1)
33
d2 := treeDiameter(edges2)
4-
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
4+
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
55
}
66

77
func treeDiameter(edges [][]int) (ans int) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
impl Solution {
2+
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
3+
let d1 = Self::tree_diameter(&edges1);
4+
let d2 = Self::tree_diameter(&edges2);
5+
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
6+
}
7+
8+
fn tree_diameter(edges: &Vec<Vec<i32>>) -> i32 {
9+
let n = edges.len() + 1;
10+
let mut g = vec![vec![]; n];
11+
for e in edges {
12+
let a = e[0] as usize;
13+
let b = e[1] as usize;
14+
g[a].push(b);
15+
g[b].push(a);
16+
}
17+
let mut ans = 0;
18+
let mut a = 0;
19+
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
20+
for &j in &g[i] {
21+
if j as isize != fa {
22+
dfs(g, j, i as isize, t + 1, ans, a);
23+
}
24+
}
25+
if *ans < t {
26+
*ans = t;
27+
*a = i;
28+
}
29+
}
30+
dfs(&g, 0, -1, 0, &mut ans, &mut a);
31+
dfs(&g, a, -1, 0, &mut ans, &mut a);
32+
ans
33+
}
34+
}

0 commit comments

Comments
 (0)