Find the length of the longest path between any two nodes in a binary tree.
Given the root of a binary tree, compute its diameter.
The diameter is the length of the longest path between any two nodes in the tree. The path does not need to pass through the root.
For this problem, the length of a path is measured by the number of edges on that path.
Example 1
Input
root = [1,2,3,4,5]
Output
3
Explanation
The longest path is 4 -> 2 -> 1 -> 3 or 5 -> 2 -> 1 -> 3, which has 3 edges.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.