Python recursion solution

  • 0
    def isValidBST(self, root):
        :type root: TreeNode
        :rtype: bool
        return self.dfs(root, -float('inf'), float('inf'))
    def dfs(self, root, l, r):
        if not root:
            return True
        if root.val <= l or root.val >= r:
            return False
        return self.dfs(root.left, l, root.val) and self.dfs(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.