java 广度搜索

280 2024-03-09 14:49

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%
相关评论
我要评论
点击我更换图片