Code for pre-order and post-order traversal in binary tree gives error, as attached in the image, while inorder is executed properly

def Inorder_traversal(self):
if self is None:
return []
return (TreeNode.Inorder_traversal(self.left)
+ [self.key]
+ TreeNode.Inorder_traversal(self.right))

def Preorder_traversal(self):
    if self is None:
        return []
    return ([self.key] 
            + TreeNode.Preorder_traversal(self.left)
            + TreeNode.Preorder_traversal(self.right))

def Postorder_traversal(self):
    if self is None:
        return []
    return (TreeNode.Postorder_traversal(self.left)
            + TreeNode.Postorder_traversal(self.right) 
            + [self.key])

Hi Tanya,
I’ve found the error in your code.

In your code just, check for the brackets, there is error in them at self.right. That’s why it is showing operand error.

Hi Vishwas

It seems the code was perfectly fine as I ran it today, it worked. Earlier upon running it many times, it showed the same error. Could possibly server’s issue.