def walk_tree ( root ) : yield root for child in root . children : for el in walk_tree ( child ) : yield el
python traverse up a tree
Pre - order depth - first
def walk_tree(root):
    """Pre-order depth-first"""
    yield root

    for child in root.children:
        for el in walk_tree(child):
            yield el