public void f(Node ndoe){
if(node == null){
return;
}
Stack<Node> stack = new Stack<>();
HashSet<Node> HashSet = new HashSet<>();
stack.add(node);
HashSet.add(node);
System.out.println(node.value);
while(!stack.isEmpty()){
Node cur = queue.poll();
for(Node next: cur.nexts){
if(!HashSet.contains(cur)){
stack.push(cur);
stack.push(next);
HashSet.add(next);
System.out.println(next.value);
break;
}
}
}
}
图的深度优先遍历
于 2022-02-23 21:43:18 首次发布