• + 0 comments

    Using Python 3’s pattern-matching:

    def getHeight(self, root):
        match root:
            case Node(left=None, right=None):
                return 0
            case Node(left=None):
                return 1 + self.getHeight(root.right)
            case Node(right=None):
                return 1 + self.getHeight(root.left)
            case _:
                return max(1 + self.getHeight(root.left), 1 + self.getHeight(root.right))