An elegant, clean python recursive solution.


  • 0
    W

    If anyone has cleaner, simpler solution, plz tell me.
    '''

    class Solution(object):
        def isValidBST(self, root):
    
            def helper(node, lower, upper):  # set boundaries for node.val.
            
                if (not node):
                    return True
                if lower < node.val < upper:
                    return helper(node.left, lower, node.val) and helper(node.right, node.val, upper)
                else:
                    return False
    
            return helper(root, -2147483649, 2147483648)  # min int and max int will be the bound for root.val

Log in to reply
 

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