Code runs fine but gives runtime error on submit

I am new to leet code so please excuse me if I am missing something obvious. I wrote my first solution and ran it. It seems to run fine and the result matches the expected result. However when I submit the code, I get a run time error. Please see the code below:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def preorderTraversal(self, root: TreeNode):
        if root.val==None:
            return [ ]
        stack,output=[root, ],[ ]
        while stack != []:
            x=stack.pop()
            if x is not None: 
                output.append(x.val)
                if x.right is not None:
                    stack.append(x.right)
                if x.left is not None:
                    stack.append(x.left)
        return output
Runtime Error Message:
Line 10: AttributeError: 'NoneType' object has no attribute 'val'
Last executed input:
[]

To be explicit, line 10 where is the error is reported is

if root.val==None:

On further attempts I figured that if I changed

if root.val==None:

to

if root is None:

the submission goes through without any errors. I don't understand why that makes a difference. I also don't understand what is the difference between running the code and submitting the code. If I get the correct output, why do I have to change the suntax for submission?

Comments (1)