Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
Created April 18, 2018 15:28
Show Gist options
  • Save hellpanderrr/137fc9d737e07ae22ed70f51e3b9d79a to your computer and use it in GitHub Desktop.
Save hellpanderrr/137fc9d737e07ae22ed70f51e3b9d79a to your computer and use it in GitHub Desktop.
python traverse n-ary tree breadth-first without recursion
from collections import deque
def bfs_traverse(root):
queue = deque([])
queue.append(root)
while (queue):
node = queue.popleft()
for child in node.children:
queue.append(child)
process_node(node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment