Matsushita's Blog

Create a Linked List of all the nodes at each depth in Binary Tree


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

How to Solve

The important point of the this problem How we should have the depth of the each node. In my case, I have node control the depth as a member in Node class. And by traversing the binary tree with breadth first search, we can make the lists. If the depth changes, we have to add a new linked list into array list. Therefore we can create linked list with different depth.

Source Code