This is the method of your class, so you must call it from an instance ( self ) or the class itself. Although it will not work, what do you think, unless you define it as a staticmethod or change your call, for example
def height(self): return 1 + max(self.left.height() if self.left is not None else 0, self.right.height() if self.right is not None else 0)
or
@staticmethod def height(self): return 1 + max(self.height(self.left) if self.left is not None else 0, self.height(self.right) if self.right is not None else 0)
Note that you should not use == to compare with None (kudos to timgeb). And you have to check if child nodes exist. And your algorithm does not work, so I changed it a bit.
Example:
class Node: def __init__(self, root=None, left=None, right=None): self.root = root self.left = left self.right = right def height(self): return 1 + max(self.left.height() if self.left is not None else 0, self.right.height() if self.right is not None else 0)
source share