Diameter of nodes

int diameterofnode(node*root){

if(root == NULL)
	return 0 ;

int ls = diameterofnode(root->left);
int rs = diameterofnode(root->right);

return max(ls,rs)+1;

}

int main()
{
node*root = build_tree();
int a1 = diameterofnode(root->left);
int a2 = diameterofnode(root->right);

cout << a1 + a2 << endl ;

}

will this code work in every test case?

hi @aggarwal.naman21 u need to do this rec for all nodes check this https://ide.codingblocks.com/s/666554

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.