Python Easy Solution


  • 0
    K
    class Solution(object):
    
    def func(self, root, start, end):
        if not root:
            return True
        if root.left:
            if root.left.val <= start or root.left.val >= root.val:
                return False
        if root.right:
            if root.right.val >= end or root.right.val <= root.val:
                return False
        tmpEnd = end
        tmpStart = start
        if root.val < end:
            tmpEnd = root.val
        if root.val > start:
            tmpStart = root.val
        return self.func(root.left, start, tmpEnd) and self.func(root.right, tmpStart, end)
        
        
        
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        return self.func(root, -9999999999999, 999999999999999)

Log in to reply
 

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