a = [] # the order in which vertices were processed q = Queue() q.put(1) # place the root at the end of the queue whilenot q.empty(): k = q.pop() # retrieve the first vertex from the queue a.append(k) # append k to the end of the sequence in which vertices were visited for y in g[k]: # g[k] is the list of all children of vertex k, sorted in ascending order q.put(y)