You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
577 B
35 lines
577 B
package class16;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.Stack;
|
|
|
|
public class Code02_DFS {
|
|
|
|
public static void dfs(Node node) {
|
|
if (node == null) {
|
|
return;
|
|
}
|
|
Stack<Node> stack = new Stack<>();
|
|
HashSet<Node> set = new HashSet<>();
|
|
stack.add(node);
|
|
set.add(node);
|
|
System.out.println(node.value);
|
|
while (!stack.isEmpty()) {
|
|
Node cur = stack.pop();
|
|
for (Node next : cur.nexts) {
|
|
if (!set.contains(next)) {
|
|
stack.push(cur);
|
|
stack.push(next);
|
|
set.add(next);
|
|
System.out.println(next.value);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|