-
-
Save hellpanderrr/137fc9d737e07ae22ed70f51e3b9d79a to your computer and use it in GitHub Desktop.
python traverse n-ary tree breadth-first without recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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