Function to print a particular level
void printGivenLevel(struct node* root, int level){
if(root == NULL)
return;
if(level == 1)
printf("%d ", root->data);
else if (level > 1)
{
printGivenLevel(root->left, level-1);
printGivenLevel(root->right, level-1);
}
}
Function to print all level order traversal
void printLevelOrder(struct node* root)
{
int h = height(root);
int i;
for(i=1; i<=h; i++)
printGivenLevel(root, i);
}
Here I want to calculate complexity of function printLevelOrder()
.
The resource from where I was referring it said it runs in O(n²)
. I can't figure out how.
Because if I apply master's subtraction theorem to calculate complexity of method 1 it comes 2ⁿ
as T(n)=2⋅T(n-1)
Please correct me, if I am approaching wrong somewhere.