Minimize the Diameter after Joining Two Trees

Quick Overview

Join two undirected trees with exactly one edge and return the minimum possible diameter. Distances count edges, and either input may be a single-node tree.

Minimize the Diameter after Joining Two Trees

Company: Tekion

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem You are given two undirected trees. The first has `n` nodes labeled `0` through `n - 1`, and the second has `m` nodes labeled `0` through `m - 1`. The rows of `edges1` and `edges2` list the edges of their respective trees. You must add exactly one edge joining a node of the first tree to a node of the second tree. Return the smallest possible diameter of the resulting tree. A tree's diameter is the greatest distance between any two nodes, measured in edges. ```cpp class Solution { public: int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2); }; ``` ### Examples **Example 1:** ```text Input: edges1 = [[0,1],[1,2],[2,3]], edges2 = [[0,1],[1,2]] Output: 4 Explanation: Joining node 1 of the first tree to node 1 of the second gives a diameter of 4. No choice of the joining edge gives a smaller diameter. ``` **Example 2:** ```text Input: edges1 = [], edges2 = [] Output: 1 Explanation: Each input is a separate one-node tree. The required joining edge makes their distance 1. ``` ### Constraints - `1 <= n, m <= 100000`. - `edges1.length == n - 1` and `edges2.length == m - 1`. - Each row in either edge list contains exactly two integers. - For every `[a_i, b_i]` in `edges1`, `0 <= a_i, b_i < n`. - For every `[u_i, v_i]` in `edges2`, `0 <= u_i, v_i < m`. - Both edge lists represent valid trees. ### Clarifying Questions - Does label `0` refer to the same node in both inputs? No. The two trees have separate node sets. - Can an input edge list be empty? Yes, when that tree has one node. - Is the answer a number of nodes or edges? Edges. - May an original edge be removed or more than one edge added? No. Keep all original edges and add exactly one joining edge. - Must the endpoints of that edge be returned? No, return only the minimum diameter. ### What a Strong Answer Covers - Considers distances both within an original tree and between the two trees. - Justifies that no different joining edge can produce a smaller answer. - Handles one-node trees and the full combined input size. ```hint Separate the possible paths A path either stays inside one original tree or crosses the new edge. How does each chosen endpoint affect the longest path of the second kind? ```

Overview: Join two undirected trees with exactly one edge and return the minimum possible diameter. Distances count edges, and either input may be a single-node tree.

Tekion logo
Tekion
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

You are given two undirected trees. The first has n nodes labeled 0 through n - 1, and the second has m nodes labeled 0 through m - 1.

The rows of edges1 and edges2 list the edges of their respective trees. You must add exactly one edge joining a node of the first tree to a node of the second tree.

Return the smallest possible diameter of the resulting tree. A tree's diameter is the greatest distance between any two nodes, measured in edges.

class Solution {
public:
    int minimumDiameterAfterMerge(vector<vector<int>>& edges1, vector<vector<int>>& edges2);
};

Examples

Example 1:

Input: edges1 = [[0,1],[1,2],[2,3]], edges2 = [[0,1],[1,2]]
Output: 4
Explanation: Joining node 1 of the first tree to node 1 of the second gives a diameter of 4. No choice of the joining edge gives a smaller diameter.

Example 2:

Input: edges1 = [], edges2 = []
Output: 1
Explanation: Each input is a separate one-node tree. The required joining edge makes their distance 1.

Constraints

  • 1 <= n, m <= 100000 .
  • edges1.length == n - 1 and edges2.length == m - 1 .
  • Each row in either edge list contains exactly two integers.
  • For every [a_i, b_i] in edges1 , 0 <= a_i, b_i < n .
  • For every [u_i, v_i] in edges2 , 0 <= u_i, v_i < m .
  • Both edge lists represent valid trees.

Clarifying Questions Guidance

  • Does label 0 refer to the same node in both inputs? No. The two trees have separate node sets.
  • Can an input edge list be empty? Yes, when that tree has one node.
  • Is the answer a number of nodes or edges? Edges.
  • May an original edge be removed or more than one edge added? No. Keep all original edges and add exactly one joining edge.
  • Must the endpoints of that edge be returned? No, return only the minimum diameter.

What a Strong Answer Covers Guidance

  • Considers distances both within an original tree and between the two trees.
  • Justifies that no different joining edge can produce a smaller answer.
  • Handles one-node trees and the full combined input size.