逐层打印二叉树与从上往下打印二叉树不同。
逐层打印二叉树的结果为:
1
2,3
4,5,6
7
void levelPrintTree(tree *T) {
if (T == null) {
return;
}
queue q;
tree *t = T;
q.push(t);
q.push(null);
while(!q.isEmpty()) {
t = q.pop();
if (t == null) {
q.push(null);
print("\n");
continue;
}
print(t->data);
if (t->lchild)
q.push(t->lchild);
if (t->rchild)
q.push(t->rchild);
}
}