master-algorithms-py/tries/trie_preorder.py
2023-08-03 16:53:01 -07:00

19 lines
365 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