呼吸优先搜索(BFS)已使用队列数据结构实现。
以下哪一项是访问上图中节点的可能顺序。
(一) MNOPQR
(B) NQMPOR
(C) QMNROP
(D) POQNMR答案: (D)
解释:在 BFS 中,我们打印一个起始节点,然后是它的相邻节点,然后是相邻节点的相邻节点,依此类推。
Option A : MNOPQR Wrong
We cannot visit "O" before "R" as we start from "M".
Note that "O" is adjacent of adjacent for "M" and
"R" is adjacent of "M".
Option B : NQMPOR Wrong
We cannot visit "P" before "O" as we start from "N".
Note that "P" is adjacent of adjacent for "N" and
"O" is adjacent.
Option C : QMNROP Wrong
We cannot visit "R" before "O" as we start from "Q".
Note that "R" is adjacent of adjacent for "Q" and
"O" is adjacent of "Q"
Option D : POQNMR Right
We visit "P", then its adjacent "O", "Q" and "N"
Finally we visit adjacent of adjacent "M" and "R"
这个问题的测验