Python solution without extra space

  • 0
    class Solution(object):
        def valid_bst(self, n):
            x  = n.val
            if n.left:
                m1,m2,valid = self.valid_bst(n.left)
                if not valid or m2 >= x:
                    return None, None, False
                m1 = x
            if n.right:
                m3, m4, valid = self.valid_bst(n.right)
                if not valid or m3 <= x:
                    return None, None, False
                m4 = x
            return m1, m4, True
        def isValidBST(self, root):
            :type root: TreeNode
            :rtype: bool
            if not root:
                return True
            _, _, valid = self.valid

Log in to reply

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