So I am solving an algoExpert.io problem, The question is to write an algorithm to calculate all branch sums left to right. The issue is the tests pass depending on how I call the helper function and I really cannot find why.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def branchHelper(root, sums=[], branchSum=0):
if root is None:
return
branchSum += root.value
if root.left is None and root.right is None:
sums.append(branchSum)
branchHelper(root.left, sums, branchSum)
branchHelper(root.right, sums, branchSum)
return sums
So this is all good so far and,
def branchSums(root):
sums = [] #
return branchHelper(root, sums) # Pass
This works fine as can be seen from this picture.

But soon as I do this (Which was my original solution):
def branchSums(root):
return branchHelper(root) # Fail
Why does using default sums=[] fail in this manner?
This is the error message. I can see that the test case used root 1 and left child 3. And my code spit out [1, 3] when I use the second method.
I really can't make sense of this, any help would be appreciated.

