mirror of https://github.com/doocs/leetcode.git
feat: add solutions to lc problem: No.3203 (#4572)
No.3203.Find Minimum Diameter After Merging Two Trees
This commit is contained in:
parent
47cb088abc
commit
4db379193e
|
|
@ -211,7 +211,7 @@ public:
|
|||
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
|
||||
d1 := treeDiameter(edges1)
|
||||
d2 := treeDiameter(edges2)
|
||||
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
|
||||
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
|
||||
}
|
||||
|
||||
func treeDiameter(edges [][]int) (ans int) {
|
||||
|
|
@ -275,6 +275,91 @@ function treeDiameter(edges: number[][]): number {
|
|||
}
|
||||
```
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
|
||||
let d1 = Self::tree_diameter(&edges1);
|
||||
let d2 = Self::tree_diameter(&edges2);
|
||||
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
|
||||
}
|
||||
|
||||
fn tree_diameter(edges: &Vec<Vec<i32>>) -> i32 {
|
||||
let n = edges.len() + 1;
|
||||
let mut g = vec![vec![]; n];
|
||||
for e in edges {
|
||||
let a = e[0] as usize;
|
||||
let b = e[1] as usize;
|
||||
g[a].push(b);
|
||||
g[b].push(a);
|
||||
}
|
||||
let mut ans = 0;
|
||||
let mut a = 0;
|
||||
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
|
||||
for &j in &g[i] {
|
||||
if j as isize != fa {
|
||||
dfs(g, j, i as isize, t + 1, ans, a);
|
||||
}
|
||||
}
|
||||
if *ans < t {
|
||||
*ans = t;
|
||||
*a = i;
|
||||
}
|
||||
}
|
||||
dfs(&g, 0, -1, 0, &mut ans, &mut a);
|
||||
dfs(&g, a, -1, 0, &mut ans, &mut a);
|
||||
ans
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### C#
|
||||
|
||||
```cs
|
||||
public class Solution {
|
||||
private List<int>[] g;
|
||||
private int ans;
|
||||
private int a;
|
||||
|
||||
public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
|
||||
int d1 = TreeDiameter(edges1);
|
||||
int d2 = TreeDiameter(edges2);
|
||||
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
|
||||
}
|
||||
|
||||
public int TreeDiameter(int[][] edges) {
|
||||
int n = edges.Length + 1;
|
||||
g = new List<int>[n];
|
||||
for (int k = 0; k < n; ++k) {
|
||||
g[k] = new List<int>();
|
||||
}
|
||||
ans = 0;
|
||||
a = 0;
|
||||
foreach (var e in edges) {
|
||||
int a = e[0], b = e[1];
|
||||
g[a].Add(b);
|
||||
g[b].Add(a);
|
||||
}
|
||||
Dfs(0, -1, 0);
|
||||
Dfs(a, -1, 0);
|
||||
return ans;
|
||||
}
|
||||
|
||||
private void Dfs(int i, int fa, int t) {
|
||||
foreach (int j in g[i]) {
|
||||
if (j != fa) {
|
||||
Dfs(j, i, t + 1);
|
||||
}
|
||||
}
|
||||
if (ans < t) {
|
||||
ans = t;
|
||||
a = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
|
|
|||
|
|
@ -209,7 +209,7 @@ public:
|
|||
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
|
||||
d1 := treeDiameter(edges1)
|
||||
d2 := treeDiameter(edges2)
|
||||
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
|
||||
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
|
||||
}
|
||||
|
||||
func treeDiameter(edges [][]int) (ans int) {
|
||||
|
|
@ -273,6 +273,91 @@ function treeDiameter(edges: number[][]): number {
|
|||
}
|
||||
```
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
|
||||
let d1 = Self::tree_diameter(&edges1);
|
||||
let d2 = Self::tree_diameter(&edges2);
|
||||
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
|
||||
}
|
||||
|
||||
fn tree_diameter(edges: &Vec<Vec<i32>>) -> i32 {
|
||||
let n = edges.len() + 1;
|
||||
let mut g = vec![vec![]; n];
|
||||
for e in edges {
|
||||
let a = e[0] as usize;
|
||||
let b = e[1] as usize;
|
||||
g[a].push(b);
|
||||
g[b].push(a);
|
||||
}
|
||||
let mut ans = 0;
|
||||
let mut a = 0;
|
||||
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
|
||||
for &j in &g[i] {
|
||||
if j as isize != fa {
|
||||
dfs(g, j, i as isize, t + 1, ans, a);
|
||||
}
|
||||
}
|
||||
if *ans < t {
|
||||
*ans = t;
|
||||
*a = i;
|
||||
}
|
||||
}
|
||||
dfs(&g, 0, -1, 0, &mut ans, &mut a);
|
||||
dfs(&g, a, -1, 0, &mut ans, &mut a);
|
||||
ans
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### C#
|
||||
|
||||
```cs
|
||||
public class Solution {
|
||||
private List<int>[] g;
|
||||
private int ans;
|
||||
private int a;
|
||||
|
||||
public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
|
||||
int d1 = TreeDiameter(edges1);
|
||||
int d2 = TreeDiameter(edges2);
|
||||
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
|
||||
}
|
||||
|
||||
public int TreeDiameter(int[][] edges) {
|
||||
int n = edges.Length + 1;
|
||||
g = new List<int>[n];
|
||||
for (int k = 0; k < n; ++k) {
|
||||
g[k] = new List<int>();
|
||||
}
|
||||
ans = 0;
|
||||
a = 0;
|
||||
foreach (var e in edges) {
|
||||
int a = e[0], b = e[1];
|
||||
g[a].Add(b);
|
||||
g[b].Add(a);
|
||||
}
|
||||
Dfs(0, -1, 0);
|
||||
Dfs(a, -1, 0);
|
||||
return ans;
|
||||
}
|
||||
|
||||
private void Dfs(int i, int fa, int t) {
|
||||
foreach (int j in g[i]) {
|
||||
if (j != fa) {
|
||||
Dfs(j, i, t + 1);
|
||||
}
|
||||
}
|
||||
if (ans < t) {
|
||||
ans = t;
|
||||
a = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
public class Solution {
|
||||
private List<int>[] g;
|
||||
private int ans;
|
||||
private int a;
|
||||
|
||||
public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
|
||||
int d1 = TreeDiameter(edges1);
|
||||
int d2 = TreeDiameter(edges2);
|
||||
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
|
||||
}
|
||||
|
||||
public int TreeDiameter(int[][] edges) {
|
||||
int n = edges.Length + 1;
|
||||
g = new List<int>[n];
|
||||
for (int k = 0; k < n; ++k) {
|
||||
g[k] = new List<int>();
|
||||
}
|
||||
ans = 0;
|
||||
a = 0;
|
||||
foreach (var e in edges) {
|
||||
int a = e[0], b = e[1];
|
||||
g[a].Add(b);
|
||||
g[b].Add(a);
|
||||
}
|
||||
Dfs(0, -1, 0);
|
||||
Dfs(a, -1, 0);
|
||||
return ans;
|
||||
}
|
||||
|
||||
private void Dfs(int i, int fa, int t) {
|
||||
foreach (int j in g[i]) {
|
||||
if (j != fa) {
|
||||
Dfs(j, i, t + 1);
|
||||
}
|
||||
}
|
||||
if (ans < t) {
|
||||
ans = t;
|
||||
a = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
|
||||
d1 := treeDiameter(edges1)
|
||||
d2 := treeDiameter(edges2)
|
||||
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
|
||||
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
|
||||
}
|
||||
|
||||
func treeDiameter(edges [][]int) (ans int) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
impl Solution {
|
||||
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
|
||||
let d1 = Self::tree_diameter(&edges1);
|
||||
let d2 = Self::tree_diameter(&edges2);
|
||||
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
|
||||
}
|
||||
|
||||
fn tree_diameter(edges: &Vec<Vec<i32>>) -> i32 {
|
||||
let n = edges.len() + 1;
|
||||
let mut g = vec![vec![]; n];
|
||||
for e in edges {
|
||||
let a = e[0] as usize;
|
||||
let b = e[1] as usize;
|
||||
g[a].push(b);
|
||||
g[b].push(a);
|
||||
}
|
||||
let mut ans = 0;
|
||||
let mut a = 0;
|
||||
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
|
||||
for &j in &g[i] {
|
||||
if j as isize != fa {
|
||||
dfs(g, j, i as isize, t + 1, ans, a);
|
||||
}
|
||||
}
|
||||
if *ans < t {
|
||||
*ans = t;
|
||||
*a = i;
|
||||
}
|
||||
}
|
||||
dfs(&g, 0, -1, 0, &mut ans, &mut a);
|
||||
dfs(&g, a, -1, 0, &mut ans, &mut a);
|
||||
ans
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue