> For the complete documentation index, see [llms.txt](https://liuzhenglaichn.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuzhenglaichn.gitbook.io/algorithm/graph/tree-diameter.md).

# Tree Diameter

## DFS Twice

* Pick a random node `i`.
* DFS to find the furthest node `j` to node `i`.
* DFS to find the furthest node `k` to node `j`.

The distance between `j` and `k` is the tree's diameter.

See [proof](https://oi-wiki.org/graph/tree-diameter/)

## Problems

* [1617. Count Subtrees With Max Distance Between Cities (Hard)](https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/)
