List<Tree<T>> unvisited = node.getChildren();
DFS:
while (!unvisited.isEmpty()) {
Tree<T> node = unvisited.remove(0);
//search node
unvisited.addAll(0, node.getChildren());
}
BFS:
while (!unvisited.isEmpty()) {
Tree<T> node = unvisited.remove(0);
//search node
unvisited.addAll(node.getChildren());
}
这些实现是不是太简单了以至于不真实?想知道我是否遗漏了什么?
最佳答案
Are these implementations too simple to be true? Was wondering if I'm missing something?
你的实现是正确的。
关于java - 深度优先搜索/广度优先搜索简单实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16378049/