Validate Binary Search Tree

Use additional stack.

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """

        if not root:
            return True

        s = collections.deque()
        prev = None
        while root or len(s) != 0:
            while root:
                s.append(root)
                root = root.left

            root = s.pop()
            if prev and root.val <= prev.val:
                return False

            prev = root
            root = root.right

        return True

results matching ""

    No results matching ""