BFS,binary search