Watch 10 video solutions for Find Minimum Diameter After Merging Two Trees, a hard level problem involving Tree, Depth-First Search, Breadth-First Search. This walkthrough by codestorywithMIK has 10,431 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |