From 141a6b5944d6f1822d972bac71591336d65fc4c9 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Thu, 3 Aug 2023 16:53:01 -0700 Subject: [PATCH] Create trie_preorder.py --- tries/trie_preorder.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tries/trie_preorder.py diff --git a/tries/trie_preorder.py b/tries/trie_preorder.py new file mode 100644 index 0000000..f72d524 --- /dev/null +++ b/tries/trie_preorder.py @@ -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