您的位置:首页 > 其它

程序10——逐层打印二叉树

2015-12-22 21:39 204 查看
逐层打印二叉树与从上往下打印二叉树不同。



逐层打印二叉树的结果为:

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: