First time posting here. Teaching myself python and was curios on how to solve the following problem using recursion. We have a company where every employee has a max of 7 reports. Given depth x of the organization, find the max number of employees including the CEO. This is basically finding the number of max nodes of a binary tree, except instead of base 2 we have base 7.
I was able to solve it linearly using the formula (b**(d+1))/(b-1) where b is the base and d is the depth of the tree.
def MaxNodes(d):
minions = ((7**(d+1)) - 1) / 6
return minions
I also solved it iteratively:
def answer(x):
minions = 1
for levels in range(x):
if (levels == 0):
minions = 7
else:
minions += (minions * 7)
return minions + 1
So we pretty much have value 1 in level 0, and starting from level 1, we start with value 7 and keep multiplying by 7 and adding to the previous result: 1 + (7x1) + (7x7) + (49x7) ... Sorry if this is very straight forward but I can't wrap my head around how to solve this recursively.
Thanks in advance for your help.