package class16; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; public class Code01_BFS { // 从node出发,进行宽度优先遍历 public static void bfs(Node start) { if (start == null) { return; } Queue queue = new LinkedList<>(); HashSet set = new HashSet<>(); queue.add(start); set.add(start); while (!queue.isEmpty()) { Node cur = queue.poll(); System.out.println(cur.value); for (Node next : cur.nexts) { if (!set.contains(next)) { set.add(next); queue.add(next); } } } } }