def my_is_sorted(node): s = stack() explored = set() curr_max = 0 s.push(node) while not s.is_empty: curr_node = s.peek() explored.add(curr_node) # Explore left subtree if curr_node.left is not None and curr_node.left not in explored: s.push(curr_node.left) continue # Compare to max if curr_node.right not in explored: if curr_node.value < curr_max: return False else: curr_max = curr_node.value # Explore right subtree if curr_node.right is not None and curr_node.right not in explored: s.push(curr_node.right) continue # All children explored s.pop() return True
def my_is_sorted(bt): s = stack() explored = set() curr_max = 0 s.push(bt) while not s.is_empty: curr_node = s.peek() explored.add(curr_node) # Explore left subtree if curr_node.left is not None and curr_node.left not in explored: s.push(curr_node.left) continue # Compare to max if curr_node.right not in explored: if curr_node.value < curr_max: return False else: curr_max = curr_node.value # Explore right subtree if curr_node.right is not None and curr_node.right not in explored: s.push(curr_node.right) continue # All children explored s.pop() return True
def run(): print "stack TEST STARTING" s = stack() i = 0 for j in xrange(0, 1000): s.push(j) val = 999 while val >= 0: x = s.pop() assert x == val val -= 1 assert s.empty() print "stack TEST COMPLETED"
def run(): print "stack TEST STARTING" s = stack() i = 0 for j in xrange(0,1000): s.push(j) val = 999 while val >= 0: x = s.pop() assert x == val val -= 1 assert s.empty() print "stack TEST COMPLETED"
def canonical_is_sorted(node): s = stack() curr_max = 0 while (not s.is_empty) or (node is not None): if node is not None: s.push(node) node = node.left else: node = s.pop() if node.value < curr_max: return False else: curr_max = node.value node = node.right return True