import java.util.*;
public class BFS {
public void breadthFirstSearch(Node root) {
Queue queue = new LinkedList<>();
Set visited = new HashSet<>();
queue.add(root);
visited.add(root);
while (!queue.isEmpty()) {
Node current = queue.poll();
System.out.println(current);
for (Node neighbor : current.getNeighbors()) {
if (!visited.contains(neighbor)) {
queue.add(neighbor);
visited.add(neighbor);
}
}
}
}
}
顶一下
(0)
0%
踩一下
(0)
0%
- 相关评论
- 我要评论
-
上一篇:返回栏目
下一篇:java cloneLIST