I am trying to calculate the runtime of a function I wrote in Javq, I wrote a function that calculates the sum of all the right children in a BT.
I used recursion in the function, and I don't really understand how to calculate the runtime in recursion non the less in a BT (just started studying the subject).
This is the code I wrote:
public int sumOfRightChildren(){
return sumOfRightChildren(this.root);
}
private int sumOfRightChildren(Node root){
if(root == null) //O(1)
return 0;//O(1)
int sum = 0;//O(1)
if(root.right != null)//O(1)
sum+=root.right.data;//O(1)
sum += sumOfRightChildren(root.right); //worst case O(n) ?
if(root.left != null)
{
sum += sumOfRightChildren(root.left);//worst case O(n)?
}
return sum;
}
I tried writing down the runtimes I think it takes, but I don't think I am doing it right. If someone can help guide me I'd be very thankful.
I'm trying to calculate T(n).