Python range based recursive solution

  • 0

    Specify valid ranges and check for validity recursively.

    def isValidBST(self, root):
            def isValid(node, leftLimit, rightLimit):
                if node == None:
                    return True
                return rightLimit > node.val >leftLimit and isValid(node.left, leftLimit, node.val) and \
                            isValid(node.right, node.val, rightLimit)
            return isValid(root, -float("inf"), float("inf"))

Log in to reply

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