[Tutor] Unsupported operand types for +: 'NoneType' and 'int' - HUH?!

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue Jun 3 19:14:02 2003


On Tue, 3 Jun 2003, Eladio Ventura wrote:

> I'm in the process of learning binary trees, and just as everything is
> going peachy, I try to make a function which calculates the total number
> of nodes and get the following error:
>
> TypeError: unsupported operand types for +: 'NoneType' and 'int'
>
> The culprit is the following function:
>
> def size(self, tree):
> 	if tree == None: return
        ^^^^^^^^^^^^^^^^^^^^^^^

> 	else:
> 		return self.size(tree.left) + 1 + self.size(tree.right)

The underlined statement looks slightly off.  size() must guarantee that
it'll return some kind of integer to work effectively.  What's the size of
the empty tree?


Good luck to you!