Diameter of Binary Tree

00:00
MediumTreeDFSRecursion
AmazonAdobe

Find the diameter of a binary tree — length of the longest path between any two nodes (may not pass through root). Path length = number of edges.

Examples

Input → [1,2,3,4,5]
Output → 3
Note: path 4→2→1→3 or 5→2→1→3
Input → [1,2]
Output → 1
Input → [1]
Output → 0