0

I'm having trouble finding the time complexity of a function. This is the function:

int Mystery(Node root){
    if(root==null)
        return null;
    if(root.leftchild==null)
        return null;
    return Mystery(root.leftchild)
}

what made me get confused is that there are 2 base conditions

this is what I wrote:

t(n)=t(n/2)+2 (2 stands for the 2 if conditions)
t(n)=t(n/4)+2+2
t(n)=t(n/8)+2+2+2
t(n)=t(n/2^k)+2*k
assume n/2^k=1
so k=log(n)
t(n)=t(1)+2log(n)
so O(log(n))

Is this correct or is there something missing?

ColdIV
  • 1,118
  • 2
  • 13
  • 21
Slim skhab
  • 13
  • 3

1 Answers1

0

There are just two base cases, but each of them imply that n has reached some small, bounded number -- assuming the tree is balanced.

So as long as the tree is balanced, your logic is correct.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413