Pyhton beats 99.77% iterative

  • 0
    class Solution(object):
        def isValidBST(self, root):
            stack = [None]
            prev = -float("inf")
            while stack:
                while root:
                    root = root.left
                x = stack.pop()
                if x:
                    if x.val <= prev:
                        return False
                    prev = x.val
                    root = x.right
            return True

Log in to reply

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