Breadth-first search! Of an arbitrary (not necessarily binary) tree. Glad to have this one. At least in the python implementation it’s weird that they model null as []
, it should be None
, but whatever.
def tree_to_list(tree_root):
res = []
worklist = [tree_root]
while worklist:
n = worklist[0]
worklist = worklist[1:]
if n == []:
continue
res.append(n.data)
for c in n.child_nodes:
worklist.append(c)
return res