X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Diameter of a Binary Tree
Easy Accuracy: 50.01% Submissions: 100k+ Points: 2

The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two end nodes. The diagram below shows two trees each with diameter nine, the leaves that form the ends of the longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).

Example 1:

Input:
1
/  \
2    3
Output: 3


Example 2:

Input:
10
/   \
20    30
/   \
40   60
Output: 4


You need to complete the function diameter() that takes root as parameter and returns the diameter.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).

Constraints:
1 <= Number of nodes <= 10000
1 <= Data of a node <= 1000

We are replacing the old Disqus forum with the new Discussions section given below.