0

I have been working on a question that calculates the sums of each branch on a binary tree and returns them in an array. Its pretty much a DFS problem in which you accumulate the solutions into an array. I am just struggling on understanding where to place the return statement in my code. I know the correct answer, I just don't know why these two snippets below aren't equivalent:

 def branchTot(root):
    soln = []
    fin  = help(root, root.value, soln)
    return fin


def help(root, sums, soln): 
    if root.left is None and root.right is None:
        soln.append(sums)
        return soln

    else:
        if root.right is not None and root.left is not None :
            help(root.left, sums + root.left.value, soln)
            help(root.right, sums + root.right.value, soln)
        elif root.right is not None:
            help(root.right, sums + root.right.value, soln)
        else:
            help(root.left, sums + root.left.value, soln)

and second solution below:

 def branchTot(root):
    soln = []
    fin  = help(root, root.value, soln)
    return fin


def help(root, sums, soln): 
    if root.left is None and root.right is None:
        soln.append(sums)


    else:
        if root.right is not None and root.left is not None :
            help(root.left, sums + root.left.value, soln)
            help(root.right, sums + root.right.value, soln)
        elif root.right is not None:
            help(root.right, sums + root.right.value, soln)
        else:
            help(root.left, sums + root.left.value, soln)

    return soln
JAVABB
  • 27
  • 5

1 Answers1

0

Both of your solutions will work in case there is a tree with just one node (root node). Now let's talk about the first solution:

  • You are returning the soln array only in case if both the children are None. Now in case where the node has one or more children, this condition will always fail. Hence, the return statement will never execute. And this is the reason, first solution is returning None. Here is the execution using Binary Search Tree.

    class Tree:
        def __init__(self, val):
            self.value = val
            self.left = None
            self.right = None
    
        def add(self, val):
            if val <= self.value:
                if self.left is None:
                    self.left = Tree(val)
                else:
                    self.left.add(val)
            else:
                if self.right is None:
                    self.right = Tree(val)
                else:
                    self.right.add(val)
    
        def t_print(self):
            if self.left is not None:
                self.left.t_print()
            print self.value
            if self.right is not None:
                self.right.t_print()
    
    def help(root, sums, soln): 
       if root.left is None and root.right is None:
           soln.append(sums)
           print 'Returning value for node ' + str(root.value)
           return soln
    
       else:
            if root.right is not None and root.left is not None :
                help(root.left, sums + root.left.value, soln)
                help(root.right, sums + root.right.value, soln)
            elif root.right is not None:
                help(root.right, sums + root.right.value, soln)
            else:
                help(root.left, sums + root.left.value, soln)
        print 'Returning none for node ' + str(root.value)
        return None     # ----> This is what being executed after every recursive call finishes (for node with children)
    
    def branchTot(root):
        soln = []
        fin  = help(root, root.value, soln)
        return fin
    

Executing above will give following output:

In [28]: root = Tree(9)
In [29]: root.add(5)
In [30]: root.add(3)
In [31]: root.add(2)
In [32]: root.add(10)
In [33]: root.add(13)
In [34]: root.add(11)


In [26]: branchTot(root)
Returning value for node 2
Returning none for node 3     ----> node with children
Returning none for node 5
Returning value for node 11    ------> node without children
Returning none for node 13
Returning none for node 10
Returning none for node 9

In [27]: 

In the second solution however, you have taken the return statement outside of the if block and therefore, the return statement is being executed no matter what. This is returning the final array.

Hope this helps.

  • I guess why I am confused is won't eventually reach the return statement because it is the base case and the previous recursive calls are moving down the tree? – JAVABB Dec 18 '19 at 22:20
  • I am confused why return of None would be after every recursive call, is it because every call on the method must have a return statement and if it doesn't then it will return None. And previous return statements are overwritten subsequent return statements? – JAVABB Dec 18 '19 at 22:27