modify the level traversal alogrithm.