I want to define a function sumOfLeftLeaves recursively:
class Node(object):
def __init__(self,x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def sumOfLeftLeaves(self,root):
if root.val == None:
return 0
elif (root.left.left == None and root.left.right == None):
return root.left.val + sumOfLeftLeaves(root.right)
else:
return sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right)
But it gives an error "NameError: global name 'sumOfLeftLeaves' is not defined", but I think it's defined recursively, what's wrong?