InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Diameter of Binary Tree

Problem Description

Given a Binary Tree A consisting of N integer nodes, you need to find the diameter of the tree.

The diameter of a tree is the number of edges on the longest path between two leaves in the tree.



Problem Constraints

0 <= N <= 105



Input Format

First and only Argument represents the root of binary tree A.



Output Format

Return an single integer denoting the diameter of the tree.



Example Input

Input 1:

           1
         /   \
        2     3
       / \
      4   5

Input 2:

            1
          /   \
         2     3
        / \     \
       4   5     6



Example Output

Output 1:

 3

Output 2:

 4



Example Explanation

Explanation 1:

 Longest Path in the tree is 4 -> 2 -> 1 -> 3 and the number of edges in this path is 3 so diameter is 3.

Explanation 2:

 Longest Path in the tree is 4 -> 2 -> 1 -> 3 -> 6 and the number of edges in this path is 4 so diameter is 4.



NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Diameter of Binary Tree on Interview Code Editor
Sign Up
to access hints and editorial solutions for Diameter of Binary Tree

Discussion


Loading...
Click here to start solving coding interview questions