Diameter of a Binary Tree | Practice | GeeksforGeeks

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

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
 
0
phantom_csin 4 hours
int find(Node *root,int &ma)
    {
        if(root==NULL)
            return 0;
        int lh=find(root->left,ma);
        int rh=find(root->right,ma);
        ma=max(ma,lh+rh+1);
        return 1+max(lh,rh);
    }
    int diameter(Node* root) {
        if(root==NULL)
            return 0;
        int ma=1;
        find(root,ma);
        return ma;
    }
0
prateek_jakhar2 days ago
JAVA SOLUTION
class Solution {
    // Function to return the diameter of a Binary Tree.
    int diameter(Node root) {
        // Your code here
        if(root==null) return 0;
        int lheight = height(root.left);
        int rheight = height(root.right);
        
        int ldiameter = diameter(root.left);
        int rdiameter = diameter(root.right);
        
        return Math.max(lheight+rheight+1, Math.max(ldiameter,rdiameter));
        
    }
    static int height(Node root){
        if(root==null) return 0;
        return Math.max(height(root.left),height(root.right))+1;
    }
}
+2
reyansh2 days ago
this problem should not be in easy problem.
+1
bipulharsh1234 days ago
time complexity: O(n^2)
space complexity: O(1)
 
void maxRad(Node *ptr, int &frad, int rad=0){       if(!ptr)           return;       rad++;       if(rad>frad)           frad = rad;       maxRad(ptr->left, frad, rad);       maxRad(ptr->right, frad, rad);   }   int diameter(Node* root) {       // Your code here       if(!root)           return 0;       int count = 1;       int leftRad=0;       int rightRad=0;       maxRad(root->left, leftRad);       maxRad(root->right, rightRad);       int total = (leftRad+rightRad+1);       int leftTotal = diameter(root->left);       int rightTotal = diameter(root->right);       return total>leftTotal?(total>rightTotal?total:rightTotal):(leftTotal>rightTotal?leftTotal:rightTotal);   }
0
pikaspark1 week ago
C++ Solution:
 
class Solution { public:   // Function to return the diameter of a Binary Tree.   int height(Node* root){       if(root == NULL)           return 0;       int ans1 = height(root->left);       int ans2 = height(root->right);       return max(1+ans1,1+ans2);   }   int diameter(Node* root) {       // Your code here       if(root == NULL)           return 0;       int ans1 = diameter(root->left);       int ans2 = diameter(root->right);              int d = 1 + height(root->left) + height(root->right);              return max(d,max(ans1,ans2));   }};
0
sikkusaurav1232 weeks ago
eassy but little bit tricky in return ---
class Solution { public: int solve(Node*root ,int &result) {     if(root==NULL)     {         return 0;     }     int l=solve(root->left,result);     int r=solve(root->right,result);     int temp=1+max(l,r);     int ans=max(temp,l+r+1);     result=max(result,ans);     return temp;           }   // Function to return the diameter of a Binary Tree.   int diameter(Node* root) {   int result=INT_MIN;          solve(root,result);   return result;      }};
0
moonviprant2 weeks ago
 pair<int,int> diameterFast(Node* root) {
        //base case
      if(root==NULL)
      {
          pair<int , int> p=make_pair(0,0);
          return p;
      }
      pair<int,int> left=diameterFast(root->left);
      pair<int,int> right=diameterFast(root->right);
      
      int op1=left.first;
      int op2=right.second;
      int op3=left.second+right.second+1;
      pair<int, int> ans;
      ans.first=max(op1,max(op2,op3));
      ans.second=max(left.second, right.second)+1;
      return ans;
      
    }
    int diameter(Node* root) {
    
        return diameterFast(root).first;
        
    }
0
1ashishchauhan20022 weeks ago
   int height(Node*root){       if(root==NULL){           return 0;       }       int l=height(root->left);       int r=height(root->right);       return max(l,r)+1;   }   int diameter(Node* root) {       if(root==NULL) return 0;      int x=diameter(root->left);      int y=diameter(root->right);      int h=height(root->left)+height(root->right)+1;      return max(h, max(x,y));               }
0
tupesanket19992 weeks ago
    int rec(Node * node,int&dia){
        if(node==NULL)return 0;
        int left = rec(node->left,dia);
        int right= rec(node->right,dia);
        dia = max(dia,left+right+1);
        return max(1+right,1+left);
    }
    int diameter(Node* root) {
        int dia=0;
        rec(root,dia);
        return dia;
    }
0
jainmuskan5653 weeks ago
  public:   int solveDiameter(struct Node * root, int &res){   // base condition   if(root == NULL){       return 0;   }   // induction    int l= solveDiameter(root->left,res);   int r= solveDiameter(root->right,res);      // conditions   int temp= 1+max(l,r);   //cout<<temp<<"<-  ";   int ans= max(temp, 1+l+r);   res= max(res,ans);   return temp;}// Function to return the diameter of a Binary Tree.int diameter(struct Node* root) {   int res= INT_MIN;   int temp=solveDiameter(root,res);   return res;}
 
/// use ampersand before res print the updated values
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.