Trains the technique from
LeetCode 3203Find Minimum Diameter After Merging Two TreesThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A campus has two footpath networks that share no paths at all. The first network has n junctions labelled 0 to n - 1; the second has m junctions labelled 0 to m - 1, numbered independently of the first. Each network is given as an edge list: edges1 holds n - 1 pairs [a, b] meaning a two-way footpath joins junctions a and b of the first network, and edges2 holds m - 1 pairs describing the second network the same way. So n is edges1.length + 1 and m is edges2.length + 1, and a network of one junction arrives as an empty edge list. Within each network every junction is reachable from every other and there are no cycles.
The walk length between two junctions is the number of footpaths on the unique route between them, and the stretch of a network is the largest walk length over all pairs of its junctions. A network of one junction has stretch 0.
Facilities will build exactly one new two-way footbridge, from some junction of the first network to some junction of the second, leaving one combined network. They may pick any such pair of endpoints. Return the smallest stretch the combined network can have.
Example 1
The first network is the chain 0-1-2-3, of stretch 3. The second has junction 1 joined to 0, 2 and 3, of stretch 2. Bridging junction 1 of the first network to junction 1 of the second gives a combined network whose furthest pair, for instance junction 3 of the first and junction 3 of the second, is the returned number of footpaths apart.
Example 2
The second network is a single junction, so its edge list is empty. The first network has two arms of three footpaths meeting at junction 0, so junctions 3 and 6 are 6 footpaths apart. Bridging the lone junction to junction 0 leaves that pair still 6 apart, which matches the returned value.
Example 3
Both networks are chains of four junctions. Bridging junction 1 of the first to junction 1 of the second makes junction 3 of the first and junction 3 of the second the furthest pair, at the returned number of footpaths.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def minimum_diameter_after_merge(edges1: list[list[int]], edges2: list[list[int]]) -> int:public int minimumDiameterAfterMerge(int[][] edges1, int[][] edges2)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.