Time Complexity

int diameter(node* root)
{
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 max(op1, max(op2, op3));

}

how is the time complexity O(N^2) for this algorithm?

hi @dare_devil_007, since you are using top-down approach ,we call for height at every node which is a O(n) operation in the worst case and since there are n nodes and n o(n) operations thus we get time complexity as O(n^2)

In case of any doubt feel free to ask :slight_smile:
mark your doubt as resolved If you got the answer

yeah got it thanks…but i am finding it very difficult in finding complexities for recursive solutions.Any idea how to be good at it

try to practice as much as u can, also there is a master theorum which is used to determine time complexities of recursive solutions
refer this :