mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create trie_bfs.py
This commit is contained in:
parent
c041716806
commit
feb21289db
28
tries/trie_bfs.py
Normal file
28
tries/trie_bfs.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
def __init__(self, val=None, children=None):
|
||||||
|
self.val = val
|
||||||
|
self.children = children
|
||||||
|
|
||||||
|
|
||||||
|
def level_orders(root):
|
||||||
|
|
||||||
|
if root is None:
|
||||||
|
return []
|
||||||
|
|
||||||
|
result = []
|
||||||
|
queue = collections.deque([root])
|
||||||
|
|
||||||
|
while queue:
|
||||||
|
level = []
|
||||||
|
for _ in range(len(queue)):
|
||||||
|
node = queue.popleft()
|
||||||
|
level.append(node.val)
|
||||||
|
queue.extend(node.children)
|
||||||
|
result.append(level)
|
||||||
|
|
||||||
|
return result
|
Loading…
x
Reference in New Issue
Block a user