The diameter of a tree tells how far apart two of its nodes can be. Look at the paths between all pairs of nodes and take the longest one. Its length is the diameter. The path does not have to go through the root. The length counts edges, not nodes.
Given the tree root, return its diameter.
How the judge passes the data
Examples
Example 1:
Input: root = [1,2,3,4,5,6]
Output: 4
Explanation: The longest path is 4 -> 2 -> 1 -> 3 -> 6. It uses 4 edges.
Example 2:
Input: root = [7,4,5,1,2,3,6,8]
Output: 5
Explanation: The longest path is 8 -> 1 -> 4 -> 7 -> 5 -> 3. It uses 5 edges.
Example 3:
Input: root = [3]
Output: 0
Explanation: With one node there is no edge, so the diameter is 0.