Description
Given the edges of an undirected tree, return its diameter. The diameter is the number of edges in the longest path between any two nodes in the tree.
Examples
Input:
edges = [[0,1],[0,2]]Output:
2Explanation:
Path 1-0-2.
Input:
edges = [[0,1],[1,2],[2,3],[2,4]]Output:
3Explanation:
The tree has node 2 as a branching point. The longest path is either 0-1-2-3 or 0-1-2-4, both having length 3.
Input:
edges = [[0,1],[1,2],[3,4],[4,5],[5,6],[2,3]]Output:
5Explanation:
Two linear chains connected: 0-1-2 and 3-4-5-6, joined at nodes 2 and 3. The diameter spans from 0 to 6: 0-1-2-3-4-5-6.
Constraints
- •
1 ≤ n ≤ 10⁴