the BFS algorithm define follow consider undirected graph vertex number 1 n initialize q new queue contain vertex 1 mark vertex 1 Extract vertex v head queue q Print index vertex v Iterate arbitrary order vertice u u neighbor v marked Mark vertex u insert tail queue q if queue continue step 2 otherwise finish since order choose neighbor vertex vary turn multiple sequence BFS printIn problem need check give sequence correspond valid bfs traversal give tree start vertex 1 the tree undirected graph exactly simple path vertex