Python Inorder solution


  • 0
    A
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    import sys
    class Solution(object):
        def isValidBST(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            if not root:
                return True
            stack = []
            done = False
            curr = root
            prev = -sys.maxsize
            while not done:
                if curr:
                    stack.append(curr)
                    curr = curr.left
                else:
                    if stack:
                        curr = stack.pop()
                        if curr.val <= prev:
                            return False
                        prev = curr.val
                        curr = curr.right
                    else:
                        done = True
            return True
    

Log in to reply
 

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