P27 · 1 second · 256 MB
Tree Diameter
Given a tree on `n` nodes as undirected `edges`, return the **number of edges** on a longest simple path (the diameter). A single node has diameter 0.
function treeDiameter(n, edges) → number
CONSTRAINTS
- 1 ≤ n ≤ 400
- edges.length === n - 1
- The edges form a tree
Input: n = 4, edges = [[0,1],[1,2],[1,3]]
Output: 2
Longest paths are 0-1-2 and 0-1-3 and 2-1-3.
Input: n = 4, edges = [[0,1],[0,2],[2,3]]
Output: 3
1-0-2-3.
1
2
3
4
5
6
7
8
Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.