Why my ans is giving wrong output in ques link given below

link->
https://practice.geeksforgeeks.org/problems/diameter-of-binary-tree/1#

my code->

int height(struct Node* root)
{
//code here
if(root==NULL)
{
return 0;
}
int h1=height(root->left);
int h2=height(root->right);
return 1+maxx(h1,h2);
}
int maxx(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int diameter(struct Node* root) {
// code here
if(root==NULL)
{
return 0;
}
int h1=height(root->left);
int h2=height(root->right);
int op1=h1+h2;
int op2=diameter(root->left);
int op3=diameter(root->right);
return maxx(op1,maxx(op2,op3));
}

Hello @raghavvij30,

You didn’t add 1 in op1,

int op1 = h1 + h2 + 1;

Here, 1 is for the root node as it will also be counted in the diameter.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.