Create trie_preorder.py

This commit is contained in:
marina 2023-08-03 16:53:01 -07:00 committed by GitHub
parent e95aa5a731
commit 141a6b5944
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

18
tries/trie_preorder.py Normal file
View 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