3203. Find Minimum Diameter After Merging Two Trees - Detailed Explanation
Problem Statement
You are given two trees, Tree1 and Tree2.
Each tree is provided as an edge list with a given number of nodes (nodes are labeled from 1 to n).
You are allowed to merge these two trees by adding one edge between any node in Tree1 and any node in Tree2.
After merging, the tree’s diameter is defined as the length of the longest shortest path between any two nodes in the merged tree.
Your task is to determine the minimum possible diameter that can be achieved by optimally choosing the two nodes to connect.
Example 1
Let Tree1 be a path of 4 nodes:
- Nodes: 1, 2, 3, 4
- Edges: (1,2), (2,3), (3,4)
- Diameter of Tree1: 3
- Radius of Tree1: ⎡3⁄2⎤ = 2
Let Tree2 also be a path of 4 nodes:
- Nodes: 1, 2, 3, 4
- Edges: (1,2), (2,3), (3,4)
- Diameter of Tree2: 3
- Radius of Tree2: 2
If you connect the “centers” of the two trees (i.e. nodes that are approximately in the middle), then the merged tree will have a diameter given by:
max( diameter(Tree1), diameter(Tree2), radius(Tree1) + radius(Tree2) + 1 )
In this case:
max(3, 3, 2 + 2 + 1) = max(3, 3, 5) = 5.
Thus, the minimum achievable diameter after merging is 5.
Example 2
Let Tree1 be a star with 4 nodes:
- Edges: (1,2), (1,3), (1,4)
- Diameter of Tree1: 2 (any two leaves are connected via the center)
- Radius of Tree1: ⎡2⁄2⎤ = 1
Let Tree2 be another star with 4 nodes:
- Edges: (1,2), (1,3), (1,4)
- Diameter of Tree2: 2
- Radius of Tree2: 1
Then the merged tree’s diameter is:
max(2, 2, 1 + 1 + 1) = max(2, 2, 3) = 3.
So the answer is 3.
Constraints
- Each tree is acyclic and connected.
- 1 ≤ number of nodes in each tree ≤ 10⁵
- The trees are given as lists of edges.
Hints
-
Diameter and Radius of a Tree:
- The diameter is the longest distance between any two nodes.
- The radius is the minimum “eccentricity” among all nodes (i.e. the minimum distance from a node to the furthest node in the tree). For any tree, it can be shown that
[ \text{radius} = \lceil \frac{\text{diameter}}{2} \rceil. ]
-
Optimal Merge Insight:
- When merging two trees by adding one edge, you can “bridge” their diameters. The best you can do is connect near the centers of each tree.
- The resulting diameter is the maximum of:
- The original diameter of Tree1.
- The original diameter of Tree2.
- The sum of the radii of Tree1 and Tree2 plus 1 (for the connecting edge).
- Therefore, the minimum possible diameter after merging is:
[ \text{Answer} = \max\Big( \text{diameter(Tree1)},\, \text{diameter(Tree2)},\, \text{radius(Tree1)} + \text{radius(Tree2)} + 1 \Big). ]
-
Computing the Diameter:
- One standard method is to perform a two-phase DFS/BFS:
- Pick an arbitrary node and find the farthest node (u).
- From (u), perform another DFS/BFS to find the farthest node (v). The distance from (u) to (v) is the diameter.
- One standard method is to perform a two-phase DFS/BFS:
Approach 1: Compute Diameters and Radii, Then Use the Merging Formula
Steps
-
For Each Tree:
- Use two DFS/BFS traversals to compute the diameter.
- Compute the radius as:
[ \text{radius} = \lceil \frac{\text{diameter}}{2} \rceil \quad \text{(which can be computed as } ( \text{diameter} + 1 ) // 2 \text{ in integer math)} ]
-
Combine the Results:
- Let (d_1) and (d_2) be the diameters of Tree1 and Tree2, and (r_1) and (r_2) be their radii.
- The minimum possible diameter after merging is:
[ \text{result} = \max( d_1,\, d_2,\, r_1 + r_2 + 1 ). ]
Why It Works
- If you connect the two trees near their centers, the extra “bridge” needed is as short as possible.
- The merged diameter must be at least as long as the diameter of either tree.
- In the “bridge” case, the new diameter can be no less than the sum of the two radii plus the connecting edge.
- Taking the maximum of these three quantities yields the minimum possible diameter over all valid merges.
Code Solutions
Python Code
Java Code
Complexity Analysis
- Time Complexity:
- Building each adjacency list takes O(n).
- Each BFS runs in O(n) (since a tree has O(n) edges).
- Therefore, computing the diameter for one tree is O(n), and for two trees O(n₁ + n₂).
- Space Complexity:
- The adjacency list and distance array require O(n) space per tree.
Step-by-Step Walkthrough
-
Compute Diameter for Each Tree:
- For Tree1, start BFS from an arbitrary node (e.g. node 1) to get a farthest node (u). Then perform BFS from (u) to obtain the diameter (d_1).
- Repeat similarly for Tree2 to obtain (d_2).
-
Compute Radii:
- Calculate (r_1 = \lceil d_1/2 \rceil) and (r_2 = \lceil d_2/2 \rceil).
-
Merge the Trees Optimally:
- The minimum merged diameter is given by
[ \max\big( d_1,\, d_2,\, r_1 + r_2 + 1 \big). ]
- The minimum merged diameter is given by
-
Return the Answer.
Common Mistakes
-
Incorrect Diameter Calculation:
Be sure to perform two BFS/DFS traversals per tree. -
Off-by-One in Radius Computation:
Use ceiling division (or equivalently, ((d + 1) // 2)) to compute the radius correctly. -
Not Considering the Connecting Edge:
Remember to add 1 to the sum of the radii for the connecting edge.
Alternative Variations and Related Problems
-
Merging More Than Two Trees:
How would the solution change if you had to merge multiple trees optimally? -
Choosing Different Connection Constraints:
What if you were allowed to add more than one edge or had a cost function associated with each edge? -
Finding Tree Centers:
Similar techniques are used when determining the center of a tree for other optimization problems. -
Related Problems:
- "Diameter of a Tree"
- "Tree Center"
- "Minimum Height Trees"
GET YOUR FREE
Coding Questions Catalog