master-algorithms-py/tries/trie_preorder.py
2023-08-08 17:02:11 -07:00

20 lines
382 B
Python

#!/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