Longest Path in a Tree

Asked at Meta · Coding Questions · Difficulty: Medium · Last asked Apr 2, 2026

You are given an undirected tree with `n` nodes labelled `0` to `n - 1`, as a list of `n - 1` edges. The distance between two nodes is the number of edges on the path between them. Find the largest distance between any two nodes of the tree (its diameter).

Loading the problem…