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
ruchitchudasama1232 hours ago
public:
    // Function to return the diameter of a Binary Tree.
    int dHelper(Node* root,int &diameter){
        if(root==NULL){
            return -1;
        }
        int lh=1+dHelper(root->left,diameter);
        int rh=1+dHelper(root->right,diameter);
        diameter=max(diameter,lh+rh);
        return max(lh,rh);
    }
    int diameter(Node* root) {
        if(root==NULL){
            return -1;
        }
        int diameter=0;
        dHelper(root,diameter);
        return diameter+1;
    }
0
utkarshagarwal10111 hours ago
class Solution {
  public:
    // Function to return the diameter of a Binary Tree.
    int height(Node* root){
        if(!root){
            return 0;
        }
        int hl = height(root->left);
        int hr = height(root->right);
        return max(hr,hl)+1;
    }
    int diameter(Node* root) {
        // Your code here
        if(!root){
            return 0;
        }
        int hl = height(root->left);
        int hr = height(root->right);
        int dia = hl+hr+1;
        int dl = diameter(root->left);
        int dr = diameter(root->right);
        return (max(max(dr,dl),dia));
    }
};
-1
1ashishchauhan200215 hours 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
phantom_cs2 days ago
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_jakhar4 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
reyansh4 days ago
this problem should not be in easy problem.
+1
bipulharsh1236 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
pikaspark2 weeks 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;
        
    }
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.