分层遍历,也可以用 vector 。
void levelTraverse(Tree root) {
Vector thisLevel = new Vector();
Vector nextLevel = new Vector();
thisLevel.add(root);
while (!thisLevel.isEmpty()) {
foreach (Tree tree in thisLevel) {
print(tree);
nextLevel.addAll(tree.children());
}
thisLevel.clear();
thisLevel.addAll(nextLevel);
nextLevel.clear();
println();
}
}