1 line recursive Python

  • 0

    Using two l and r to represent the left and right boundaries for root.val.

    class Solution(object):
        def isValidBST(self, root, l = float('-inf'), r = float('inf')):
            :type root: TreeNode
            :rtype: bool
            return not root or (l < root.val < r and self.isValidBST(root.left, \
                   l, root.val) and self.isValidBST(root.right, root.val, r))

Log in to reply

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