Diameter of Binary Tree

easyTreesDFS returning height while tracking max diameterSolve on LeetCode#543

Part of Practice: Recursive Foundations

Given the root of a binary tree, return the diameter: the number of edges on the longest path between any two nodes in the tree. The path does not have to pass through the root — it can live entirely inside one subtree.

The tree has 1 to 10,000 nodes. Note the answer counts edges, not nodes: a path of 4 nodes has length 3.

Examples

Input:
root = [1,2,3,4,5]
Output:
3

The longest path is 4 → 2 → 5 or 4 → 2 → 1 → 3, both spanning 3 edges.

Input:
root = [1,2]
Output:
1
Input:
root = [1]
Output:
0

A single node has no edges, so the diameter is 0.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.