Skip to content

3203. Find Minimum Diameter After Merging Two Trees 👍

  • Time: $O(n + m)$
  • Space: $O(n + m)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
 public:
  int minimumDiameterAfterMerge(vector<vector<int>>& edges1,
                                vector<vector<int>>& edges2) {
    const int diameter1 = getDiameter(edges1);
    const int diameter2 = getDiameter(edges2);
    const int combinedDiameter = (diameter1 + 1) / 2 + (diameter2 + 1) / 2 + 1;
    return max({diameter1, diameter2, combinedDiameter});
  }

 private:
  int getDiameter(const vector<vector<int>>& edges) {
    const int n = edges.size() + 1;
    vector<vector<int>> graph(n);

    for (const vector<int>& edge : edges) {
      const int u = edge[0];
      const int v = edge[1];
      graph[u].push_back(v);
      graph[v].push_back(u);
    }

    int maxDiameter = 0;
    maxDepth(graph, 0, /*prev=*/-1, maxDiameter);
    return maxDiameter;
  }

  // Similar to 1522. Diameter of N-Ary Tree
  // Returns the maximum depth of the subtree rooted at u.
  int maxDepth(const vector<vector<int>>& graph, int u, int prev,
               int& maxDiameter) {
    int maxSubDepth1 = 0;
    int maxSubDepth2 = 0;
    for (const int v : graph[u]) {
      if (v == prev)
        continue;
      const int maxSubDepth = maxDepth(graph, v, u, maxDiameter);
      if (maxSubDepth > maxSubDepth1) {
        maxSubDepth2 = maxSubDepth1;
        maxSubDepth1 = maxSubDepth;
      } else if (maxSubDepth > maxSubDepth2) {
        maxSubDepth2 = maxSubDepth;
      }
    }
    maxDiameter = max(maxDiameter, maxSubDepth1 + maxSubDepth2);
    return 1 + maxSubDepth1;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
  public int minimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
    final int diameter1 = getDiameter(edges1);
    final int diameter2 = getDiameter(edges2);
    final int combinedDiameter = (diameter1 + 1) / 2 + (diameter2 + 1) / 2 + 1;
    return Math.max(Math.max(diameter1, diameter2), combinedDiameter);
  }

  private int getDiameter(int[][] edges) {
    final int n = edges.length + 1;
    List<Integer>[] graph = new List[n];

    for (int i = 0; i < n; i++)
      graph[i] = new ArrayList<>();

    for (int[] edge : edges) {
      final int u = edge[0];
      final int v = edge[1];
      graph[u].add(v);
      graph[v].add(u);
    }

    int[] maxDiameter = new int[1];
    maxDepth(graph, 0, -1, maxDiameter);
    return maxDiameter[0];
  }

  // Similar to 1522. Diameter of N-Ary Tree
  // Returns the maximum depth of the subtree rooted at u.
  private int maxDepth(List<Integer>[] graph, int u, int prev, int[] maxDiameter) {
    int maxSubDepth1 = 0;
    int maxSubDepth2 = 0;
    for (final int v : graph[u]) {
      if (v == prev)
        continue;
      final int maxSubDepth = maxDepth(graph, v, u, maxDiameter);
      if (maxSubDepth > maxSubDepth1) {
        maxSubDepth2 = maxSubDepth1;
        maxSubDepth1 = maxSubDepth;
      } else if (maxSubDepth > maxSubDepth2) {
        maxSubDepth2 = maxSubDepth;
      }
    }
    maxDiameter[0] = Math.max(maxDiameter[0], maxSubDepth1 + maxSubDepth2);
    return 1 + maxSubDepth1;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution:
  def minimumDiameterAfterMerge(
      self,
      edges1: list[list[int]],
      edges2: list[list[int]],
  ) -> int:
    diameter1 = self._getDiameter(edges1)
    diameter2 = self._getDiameter(edges2)
    combinedDiameter = (diameter1 + 1) // 2 + (diameter2 + 1) // 2 + 1
    return max(diameter1, diameter2, combinedDiameter)

  def _getDiameter(self, edges: list[list[int]]) -> int:
    n = len(edges) + 1
    graph = [[] for _ in range(n)]

    for u, v in edges:
      graph[u].append(v)
      graph[v].append(u)

    maxDiameter = [0]
    self._maxDepth(graph, 0, -1, maxDiameter)
    return maxDiameter[0]

  # Similar to 1522. Diameter of N-Ary Tree
  def _maxDepth(
      self,
      graph: list[list[int]],
      u: int,
      prev: int,
      maxDiameter: list[int],
  ) -> int:
    """Returns the maximum depth of the subtree rooted at u."""
    maxSubDepth1 = 0
    maxSubDepth2 = 0
    for v in graph[u]:
      if v == prev:
        continue
      maxSubDepth = self._maxDepth(graph, v, u, maxDiameter)
      if maxSubDepth > maxSubDepth1:
        maxSubDepth2 = maxSubDepth1
        maxSubDepth1 = maxSubDepth
      elif maxSubDepth > maxSubDepth2:
        maxSubDepth2 = maxSubDepth
    maxDiameter[0] = max(maxDiameter[0], maxSubDepth1 + maxSubDepth2)
    return 1 + maxSubDepth1