mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-17 03:19:22 -04:00
Create trie_preorder.py
This commit is contained in:
parent
e95aa5a731
commit
141a6b5944
1 changed files with 18 additions and 0 deletions
18
tries/trie_preorder.py
Normal file
18
tries/trie_preorder.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
|
def preorder(root: 'Node'):
|
||||||
|
|
||||||
|
if root is None:
|
||||||
|
return []
|
||||||
|
|
||||||
|
stack, result = [root, ], []
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
node = stack.pop()
|
||||||
|
result.append(node.val)
|
||||||
|
stack.extend(node.children[::-1])
|
||||||
|
|
||||||
|
return result
|
Loading…
Add table
Add a link
Reference in a new issue