You are given the root of a binary tree. Your task is to find the diameter of the tree.
The diameter of a binary tree is defined as:
Return the diameter of the binary tree.
Input: root = [1,2,3,4,5]
Output: 3
Explanation: The longest path is 4 → 2 → 1 → 3, which contains 3 edges.
Input: root = [1,2]
Output: 1
Explanation: The longest path connects the two nodes, resulting in a diameter of 1 edge.
Input: root = [1]
Output: 0
Explanation: A single-node tree has no edges, so its diameter is 0.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1
0
1 2 3 4 5
3