You are given n as the number of nodes and a 2D array of edges representing a tree having the following properties:
-
It is an acyclic, undirected graph containing n nodes and n−1 edges.
-
The nodes are labeled from 0 to n−1.
-
An edge at the ith index is represented as [ai,bi], which denotes that there is an undirected edge connecting the nodes ai and bi.
Your task is to find all possible root nodes that can be used to create trees of minimum height. The height of a tree is the length of the longest path from the root node to any leaf node.
Note: The order of root nodes in the result does not matter.
Constraints
- 1≤n≤500
edges.length ==n−1
- ai=bi
- Each edge is unique.
So far, you have probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
A naive approach is to use Depth-first Search (DFS) to ...