Skip to content

q [terminology]

Terminology

Usage

  • Use for a single queue in a compact BFS implementation.
  • Prefer queue when several queues or worklists coexist.
  • Keep queue-specific operations close enough that the data structure remains obvious.
res = []
q = deque([root])
while q:
    level = []
    for _ in range(len(q)):
        node = q.popleft()
        level.append(node.val)
        # ...
    res.append(level)

See 102. Binary Tree Level Order Traversal.