mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
20 lines
382 B
Python
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
|