educative.io

Educative

Iterative solution

Let’s use stack implicitly:

def is_bst(node):
  cv = None
  stack = []
  while node or stack:
    if node:
      stack.append(node)
      node = node.left
    else:
      node = stack.pop()
      if cv is not None and node.data < cv:
        return False
      cv = node.data
      node = node.right

  return True

Hi @Vladimir_Ryabtsev,

Thank you so much for reaching out to us.

Yes, we can use a solution with a loop that involves another data structure Stack, but our Stack is automatically managed by the OS with the recursive solution. You can use either of the solutions if there isn’t any restriction.

If you have any other queries, then please do let us know. Thank you.