Easy

Quiz

#543 Diameter of Binary Tree

APPROACH

Given the root of a binary tree, return the tree's diameter — the number of edges along the longest path connecting any two nodes. This path does not need to go through the root.

Example 1:

Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].

Example 2:

Input: root = [1,2]
Output: 1
1 of 4
1:00

What is the optimal approach for this problem?