Implemented BFS level algorithm.