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.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.
C++
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.
Time Complexity: O(n + m) for calculating diameters using BFS.
Space Complexity: O(n + m) due to using adjacency lists for the graphs.
| 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. |
L16. Diameter of Binary Tree | C++ | Java • take U forward • 438,921 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