Originally Posted by

**Snafuist**
No, you only need to visit every node exactly once. Consider how BFS works:

1. Enqueue the root node.

2. Dequeue a node.

3. Enqueue any of its successors.

4. If the queue is empty, every node on the graph has been visited -- quit

5. Repeat from Step 2.

(add the search part as appropriate)

In order to keep track of the current depth, you do the following: mark the root node as being white. In step 3, mark the children of white nodes as being black, the children of black nodes as being white. In step 2, if you pop a node with a different color than the previous one, you increase the depth by 1. The marking is done in constant time and every node is visited exactly once. Regarding the runtime, there's no difference to the original BFS, except that you now know the current depth.

Greets,

Philip