easyTreesPure DSA~14 min
Org Span Diameter
Given a reporting tree, find the longest communication path between any two people — the number of reporting edges on the longest path, which need not pass through the CEO.
Problem
Given the root of a binary tree, return the length of its diameter: the number of edges on the longest path between any two nodes in the tree. The path may or may not pass through the root.
Input
A binary tree root (1 ≤ nodes ≤ 10^4).
Output
An integer — the diameter measured in edges.
Constraints
- Diameter is counted in edges, not nodes
- The longest path need not include the root
- Single traversal expected
Examples
Example 1
Input
root = [1, 2, 3, 4, 5]
Output
3
Path 4 → 2 → 1 → 3 (or 5 → 2 → 1 → 3) has 3 edges.
Example 2
Input
root = [1, 2]
Output
1
One edge between the two nodes.