mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
21 lines
411 B
Python
21 lines
411 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
def bfs(root) -> list:
|
|
|
|
result = []
|
|
queue = collections.deque([root])
|
|
|
|
while queue:
|
|
|
|
node = queue.popleft()
|
|
|
|
if node:
|
|
result.append(node.val)
|
|
queue.append(node.left)
|
|
queue.append(node.right)
|
|
|
|
return result
|