Anyone know this error.. AttributeError: 'NoneType' object has no attribute 'val'


  • 0
    L

    I got below errors

    AttributeError: 'NoneType' object has no attribute 'val'

    I am using below codes and it works on my mac. any idea?

    class Solution:
        # @param root, a tree node
        # @return a list of integers
        def preorderTraversal(self, root):
            stack = []
            result = []
            stack.append(root)
    
            while len(stack) != 0 :
               node = stack.pop()
               result.append(node.val)
               print "Node value " + repr(node)    
               print "Node value val " + repr(node.val)
               print "Node value L " + repr(node.left)
               print "Node value R " + repr(node.right)
               if node.right != None : 
                   stack.append(node.right)
                   print "Append R" 
               if node.left != None :                      
                   stack.append(node.left)                 
                   print "Append L"
    
            return result

  • 0
    L

    fixed.
    need to check root is None case.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.