0

I am trying to write a code to print the path in a Binary Tree which add up to the sum passed to the function. To which the below code keeps on failing for a simple test case:

Last executed input: Binary Tree = [1], sum = 1

Runtime Error Message: Line 26: TypeError: object of type 'NoneType' has no len()

Stdout:None None

I cannot understand how the leftPath and rightPath are becoming None. I am not returning None at all.

'''
Created on Aug 12, 2015

@author: debpriyas
'''

class BTNode(object):
    '''
    classdocs
    '''
    def __init__(self,value, leftBTNode=None, rightBTNode=None):
        '''
        Constructor
        '''
        self.value = value
        self.left = leftBTNode
        self.right = rightBTNode
        # To deal with duplicate values in BST. 
        self.count = 1

def pathSum(root, sum):
    return pathSumHelper( root, sum, [])

def pathSumHelper(root, sum, path):

    if root == None:
        if sum == 0:
            return path
        else:
            return []

    leftPath = pathSumHelper(root.left, sum-root.value, path.append(root.value))
    rightPath = pathSumHelper(root.right, sum-root.value, path.append(root.value))
    print leftPath, rightPath
    if len(leftPath) == 0 and len(rightPath) == 0:
        return []

    if len(leftPath) == 0:
        return [rightPath]
    elif len(rightPath) == 0:
        return [leftPath]
    return [leftPath, rightPath]


if __name__ == '__main__':
    root = BTNode(1)

    print pathSum(root, 1)
2
  • Please try to convert your code snipped into a minimal, complete, and verifiable example. Commented Aug 22, 2015 at 21:42
  • @das-g: Changed code which can be ran on isolated system. The code was minimal and complete earlier as well. Now i guess its verifiable. Commented Aug 22, 2015 at 23:15

1 Answer 1

2

The problem is here:

leftPath = pathSumHelper(root.left, sum-root.value, path.append(root.value))
rightPath = pathSumHelper(root.right, sum-root.value, path.append(root.value))

You are using path.append(root.value) and then using what is returned from the "append" function call as an argument in the pathSumHelper function, which is a NoneType (it modifies the object in-place, and returns None, which is why when you call it from the console, it returns nothing).

Rather, you need to use append prior to the function call and then use path in the function or do

path + [root.value]

in your function call so it will return an actual list.

I would recommend doing the following since you want to modify "path" in-place I am assuming.

path.append(root.value)
leftPath = pathSumHelper(root.left, sum-root.value, path)

Anything where the sum is 0, it will then return a Nonetype, which will cause a TypeError during the len call.

A simple, verifiable, reproducible example is:

>>>def a(x):
...    print(x)

>>>mylist = list(range(10))
>>> a(mylist.append(1))
None

>>> a(mylist + [5])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 5]
Sign up to request clarification or add additional context in comments.

4 Comments

I completely forgot that it's a method and would return a value. Now it makes sense. Now looking back it looks obvious. Thanks @Alexander Huszagh .
Of course, if I've helped you and that answers your question and you feel there are no better answers, please feel free to accept it as an answer. Glad to help.
How to accept the answer. I cannot upvote because of very less points. Is there any other way to accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.