There exist two undirected trees with n and m nodes, numbered from 0 to n - 1 and from 0 to m - 1, respectively. You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree.
You must connect one node from the first tree with another node from the second tree with an edge.
Return the minimum possible diameter of the resulting tree.
The diameter of a tree is the length of the longest path between any two nodes in the tree.
Example 1:
Input: edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]
Output: 3
Explanation:
We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.
Example 2:
Input: edges1 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]], edges2 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]]
Output: 5
Explanation:
We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.
Constraints:
1 <= n, m <= 105edges1.length == n - 1edges2.length == m - 1edges1[i].length == edges2[i].length == 2edges1[i] = [ai, bi]0 <= ai, bi < nedges2[i] = [ui, vi]0 <= ui, vi < medges1 and edges2 represent valid trees.Problem Overview: Two separate trees are given. You can connect any node from the first tree to any node from the second tree with a single edge. The task is to choose the connection that produces the smallest possible diameter of the resulting tree.
Approach 1: Tree Diameter Calculation (O(n + m) time, O(n + m) space)
This approach relies on a key property of trees: the best way to minimize the resulting diameter is to connect the centers of the two trees. Start by computing the diameter of each tree independently using two DFS/BFS passes. First traversal finds the farthest node from any starting node, and the second traversal from that node gives the diameter length.
Once you know the diameter d, the tree's radius is ceil(d / 2). When two trees are merged optimally, the new diameter becomes the maximum of three values: d1, d2, and ceil(d1/2) + ceil(d2/2) + 1. The first two represent the original diameters that might still dominate, while the third represents the path that travels through the newly added edge. This method only requires building adjacency lists and running DFS/BFS traversals. It works naturally with tree and breadth-first search or depth-first search techniques.
Approach 2: Dynamic Programming and Merging Strategy (O(n + m) time, O(n + m) space)
This version computes subtree heights and longest paths using dynamic programming on trees. During traversal, track the two largest child heights for every node to maintain the longest path passing through that node. This gives the diameter of each tree without explicitly performing the classic double BFS trick.
After computing diameters and node heights, derive each tree's effective radius from the DP results. The optimal merge again connects the best center-like nodes, producing the same formula for the final diameter: max(d1, d2, r1 + r2 + 1). This approach is useful when you already maintain DP state on trees or need to extend the solution with additional per-node calculations.
Recommended for interviews: The tree diameter approach using two BFS/DFS passes is the expected solution. It shows you understand tree diameter properties and how centers minimize path lengths. The DP variant demonstrates deeper mastery of graph traversal and tree DP, but most interviewers look for the clean diameter + radius insight.
This approach involves calculating the diameter of each tree individually. The key observation is that connecting two trees will increase the diameter if the added edge connects the nodes that already are part of their tree's longest path (diameter ends). Therefore, first calculate the diameter of each tree and then consider possible merging nodes to find the minimum resulting diameter.
The function tree_diameter() calculates the diameter of a tree using BFS starting from an arbitrary node, finding the farthest node from it, and then conducting one more BFS from this farthest node to get the diameter. The main function min_tree_diameter() then uses these diameters to compute the smallest possible diameter according to merging strategies while considering the worst-case longest path in the resulting tree.
Time Complexity: O(n + m), where n and m are the number of nodes in each tree respectively as BFS runs in linear time with respect to the number of edges.
Space Complexity: O(n + m) for storing the graph representation.
This approach involves using dynamic programming to calculate subtree information and merge the trees in an optimal manner to achieve a minimum diameter.
The Graph class constructs an adjacency list and provides a method to calculate the diameter via BFS. The main method minTreeDiameter() uses two graphs to compute diameters and then finds the minimum possible diameter after merging the trees.
Java
Time Complexity: O(n + m) for calculating diameters using BFS.
Space Complexity: O(n + m) due to using adjacency lists for the graphs.
We denote d_1 and d_2 as the diameters of the two trees, respectively. Then, the diameter of the merged tree can be one of the following two cases:
max(d_1, d_2);r_1 = \lceil \frac{d_1}{2} \rceil and r_2 = \lceil \frac{d_2}{2} \rceil, respectively. Then, the diameter of the merged tree is r_1 + r_2 + 1.We take the maximum of these two cases.
When calculating the diameter of a tree, we can use two DFS passes. First, we arbitrarily select a node and start a DFS from this node to find the farthest node from it, denoted as node a. Then, we start another DFS from node a to find the farthest node from node a, denoted as node b. It can be proven that the path between node a and node b is the diameter of the tree.
The time complexity is O(n + m), and the space complexity is O(n + m), where n and m are the number of nodes in the two trees, respectively.
| Approach | Complexity |
|---|---|
| Approach using Tree Diameter Calculation | Time Complexity: O(n + m), where n and m are the number of nodes in each tree respectively as BFS runs in linear time with respect to the number of edges. |
| Approach using Dynamic Programming and Merging Strategy | Time Complexity: O(n + m) for calculating diameters using BFS. |
| Two DFS Passes | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Tree Diameter Calculation (DFS/BFS) | O(n + m) | O(n + m) | Best general solution. Simple two-pass traversal per tree. |
| Tree DP with Height Tracking | O(n + m) | O(n + m) | Useful when already performing dynamic programming on tree structures. |
Find Minimum Diameter After Merging Two Trees | Leetcode 3203 | Graph Concepts & Qns - 45 | MIK • codestorywithMIK • 10,431 views views
Watch 9 more video solutions →Practice Find Minimum Diameter After Merging Two Trees with our built-in code editor and test cases.
Practice on FleetCode