Help me out in this

@KartikKhariwal leave this i got my error

okay @shivammishra20121999

@Kartikkhariwal1 what is my timecomplexity of this?

int countNodes(TreeNode *root)
{
    TreeNode *pleft=root;
    TreeNode* pright=root;
    int count1=0;
    while(pleft!=NULL)
    {
        ++count1;
       pleft=pleft->left;
        
    }
    int count2=0;
    while(pright!=NULL)
    {
        ++count2;
        pright=pright->right;
    }
    if(count1==count2)
    {
        return (int)pow(2,count1)-1;
    }
    return 1+countNodes(root->left)+countNodes(root->right);
}

};

@Kartikkhariwal1 how to calculate?

Hey @shivammishra20121999
There is no simple way to calculate Time complexity of ur code, but if u look at it broadly then its O(nlog(n)) when u will traverse all nodes and check left and right heights. But that will not be the case always since its a CBT So it will be better than O(nlogn) u can say .
If u have used simple recursion then its O(n)
1+count(root->l)+count(root->r)

@Kartikkhariwal1 thks kartik

1 Like