Error
|
|
---|---|
@Ibrahim Nash | 6420 |
@blackshadows | 6376 |
@mb1973 | 5604 |
@Quandray | 5231 |
@akhayrutdinov | 5111 |
@saiujwal13083 | 4510 |
@sanjay05 | 3762 |
@kirtidee18 | 3673 |
@marius_valentin_dragoi | 3522 |
@sushant_a | 3459 |
@verma_ji | 3413 |
Complete Leaderboard | |
|
|
@codersgroup18 | 799 |
@sriramgoparaju99 | 737 |
@Manikanta punnam | 629 |
@rdakka | 606 |
@prankursharma31 | 597 |
@praveenbgp6 | 546 |
@sanskar94511 | 531 |
@purohitmn02 | 529 |
@yashkaril4 | 517 |
@abhikarshgupta | 474 |
@rohitgarg2825 | 464 |
Complete Leaderboard |
Given a Binary Tree, find diameter of it.
The diameter of a tree is the number of nodes on the longest path between two end nodes in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a 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
Your Task:
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 strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes