Search⌘ K
AI Features

Solution: Find Minimum Diameter After Merging Two Trees

Solution: Find Minimum Diameter After Merging Two Trees gives two undirected trees and asks you to connect one node from each with an edge, then return the smallest diameter of the combined tree.

Statement

You are given two undirected trees: one with nn nodes labeled from 00 to n−1n - 1, and another with mm nodes labeled from 00 to m−1m - 1. Their structures are defined by two 2D2D integer arrays—edges1 of length n−1n - 1 for the first tree, and edges2 of length m−1m - 1 for the second. Each element edges1[i] = [aᵢ, bᵢ] represents an edge between nodes aᵢaᵢ and bᵢbᵢ in the first tree, and similarly, edges2[i] = [uᵢ, vᵢ] represents an edge in the second tree.

Your task is to connect any node from the first tree to any one node from the second tree using a single edge. ...