Simple recursive Python solution

  • 0
    class Solution:
        # @param root, a tree node
        # @return a boolean
        def isValidBST(self, root):
            if root == None:
                return True
            def valid(root, maximum, minimum):
                if root.val >= maximum or root.val <= minimum:
                    return False
                left, right = True, True
                if root.left != None:
                    left = valid(root.left, root.val, minimum)
                if root.right != None:
                    right = valid(root.right, maximum, root.val)
                return left and right
            return valid(root, float("inf"), float("-inf"))

  • 0
    This post is deleted!

Log in to reply

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