155. Diameter of Binary Tree

Easy · Binary Tree

Find the diameter of a binary tree — the longest path between any two nodes, measured in edges.

You are given a binary tree as an array using level-order (breadth-first) serialization, where `null` represents a missing node. For example, `[1,2,3,4,5]` represents a tree with root 1, left child 2, right child 3, and 2's children 4 and 5.

Return a single integer: the number of edges in the longest path between any two nodes in the tree. This path does not need to pass through the root.

Examples

Example 1
Input: [1,2,3,4,5]
Output: 3
Explanation: Longest path 4-2-1-3 or 5-2-1-3 has 3 edges
Example 2
Input: [1,2]
Output: 1
Explanation: One edge between root and child

Constraints