Medium · Trees

Get diameter

Given the root of a binary tree, return its diameter: the number of edges on the longest path between any two nodes (0 for an empty or single-node tree).

Examples

Example 1

[1,2,3,4,5]

Output: diameter 3

Example 2

{
  "tree": [1, 2, null, 3]
}

Output: diameter 2

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems