Data Structures19 sections · 729 units
Open in Course

Problem - Diameter of Binary Tree

Longest path in tree

Given the root of a binary tree, return the length of the diameter. The diameter is the length of the longest path between any two nodes.

This path may or may not pass through the root. Example: For tree [1,2,3,4,5][1,2,3,4,5], the diameter is 3 (path 42134 \to 2 \to 1 \to 3 or 52135 \to 2 \to 1 \to 3).

The length of a path is the number of edges, not nodes. Constraints: up to 10410^4 nodes, values from 100-100 to 100100.