# List of Depths

Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e. if you have a tree with depth D, you’ll have D linked lists).

#### Solution

Let’s use a breadth-first search to traverse the tree. Using the BFS, we can visit each level of the tree. We will leverage the queue to keep track of the level of the binary tree which we are visiting. When we visit a level of the tree, we will copy the queue to a temporary queue so that we can repopulate the original queue with the children of all nodes at that tree height.

We will visit all nodes in the temporary queue and do two things: append their values to a linked list which represents all nodes at that height, and append their children to the queue. In this way, we can have the algorithm visit each level of the tree, sequentially.

def create_lists(node):
queue = []
results = []

queue.append(node)

# create linked list with only the root

while(queue):
# copy queue to temporary queue
parents = queue
queue = []

for parent in parents:
if(parent.left != None):
queue.append(parent.left)
if(parent.right != None):
queue.append(parent.right)

return results


We can also implement a pre-order traversal, to visit the root node, and then all children nodes.

def pre_order_traversal(node, results, level):
if(node == None):
return

if(len(results) == level):