I have an input dictionary that describes a tree structure, eg:
{'title': 'Root', 'children': [
{'title': 'Child 1','children': [
{'title': 'Grandchild 11', 'children': [
{'title': 'Great Grandchild 111', 'children': []}
]}
]},
{'title': 'Child 2', 'children': [
{'title': 'Grandchild 21', 'children': []}
]},
{'title': 'Child 3', 'children': [
{'title': 'Grandchild 31', 'children': []}
]}
]}
I am trying to write a python function that flattens this structure into a list. The list should contain one int per object in this dictionary, where the integer represents the index of that object's parent in the parent's own 'generation'. The traversal of the tree should be level-order.
In the case of the example above, the expected output would be
[0, 0, 0, 0, 0, 1, 2, 0]
... as the parent indices corresponding to the objects titled:
['Root', 'Child 1', 'Child 2', 'Child 3', 'Grandchild 11', 'Grandchild 21', 'Grandchild 31', 'Great Grandchild 111']
This feels like a Lisp problem to me and Python recursion has me hung up.