如果这个问题听起来模棱两可,我很抱歉,但我在采访中被问到了这个问题。
为图/树中的 BFS 编写一个程序。
我使用队列编写了流行的代码。
现在他要求我通过修改我刚刚编写的 BFS 代码的一行来将其转换为 DFS 代码。
我能想到的唯一答案是使用堆栈进行 DFS。然后我使用 2 个队列实现了堆栈。
所以最后我的答案是:使用1个队列进行BFS。对于 DFS,请改用 2 个队列。
他没有给我任何反馈。没有被录用:(
我的方法好还是有更好的方法?请帮忙。:)
algorithm tree graph data-structures
algorithm ×1
data-structures ×1
graph ×1
tree ×1