I have a tree of numbers that I want to be able to find the sum of numbers. Below each number are two children to the left and right Of all the possible paths, I want to be able to find the biggest number through all the possible paths. Here is an example
8
3 11
10 2 32 6
returns 8+11+32=51
I feel that this is a recursion problem but I am stuck with my code and keep getting errors. I think that I am approaching this incorrectly. Below is my code:
# Returns root key value
def getRootValue(root):
return root
# Returns reference to left child
def getLeftChild(root):
value=None
if root.leftChild!=None:
value=root.leftChild
return value
# Returns reference to right child
def getRightChild(root):
value=None
if root.rightChild!=None:
value = root.rightChild
return value
def sum_of_branch(root):
sum=0
if root.getLeftChild() ==None and root.getRightChild()==None:
return rounds
else:
rounds+=rounds+1
keys_sum[depth]=sum+root.key
return sum_to_deepest(root.left), sum_to_deepest(root.right)
if root.getLeftChild()!=None:
rounds+=root.getLeftChild().branchLenSum()
if root.getRightChild()!=None:
rounds+=root.getRightChild().branchLenSum()
return rounds