Widest Span
easyYou're stringing festival lights through a tree and want the single longest run of cable that follows branches — between any two nodes, not necessarily through the trunk. Each branch segment (edge) is one meter.
Given the root of a binary tree, return its diameter: the number of edges on the longest path between any two nodes in the tree.
Tests pass a level-order array; implement the core in _diameter on real nodes.
Example 1
in values = [1, 2, 3, 4, 5]
out 3
Longest path: 4 → 2 → 5 won't do (2 edges); 4 → 2 → 1 → 3 has 3 edges.
Example 2
in values = [1, 2]
out 1
Example 3
in values = [1]
out 0
Constraints
- · 0 <= number of nodes <= 10^4
- · -100 <= node value <= 100
- · diameter counts edges, not nodes
solution.py● loading python…
test results
Hit ▶ Run to test your code against the visible cases.