Loading...
You are given an unweighted tree with n nodes labeled 1 to n, described by an array edges of its n - 1 undirected edges, where edges[i] = [a, b].
n
1
edges
n - 1
edges[i] = [a, b]
The diameter of the tree is the maximum number of edges on a path between any two nodes.
Return the diameter.
Input: n = 5, edges = [[1,2],[1,3],[3,4],[3,5]] Output: 3 Explanation: The path 2 - 1 - 3 - 4 (or 2 - 1 - 3 - 5) uses 3 edges, and no path is longer.
Input: n = 1, edges = [] Output: 0 Explanation: A single node has no path to any other node.
edges.length
Click "Run" to test with sample cases or "Submit" to run all tests.