发布网友 发布时间:2022-04-25 12:42
共2个回答
热心网友 时间:2024-11-03 15:11
用层次遍历二叉树的代码如下:
void print(tree& t) {
if(t == NULL) return;
tree p;
tree qu[n];
int front, rear;
front = rear = -1;
rear++;
qu[rear] = t;
while (front != rear) {
front = (front + 1) % n;
p = qu[front];
cout<<p->data<<" ";
if (p->L != NULL) {
rear = (rear + 1) % n;
qu[rear] = p->L;
}
if (p->R != NULL) {
rear = (rear + 1) % n;
qu[rear] = p->R;
}
}
}
热心网友 时间:2024-11-03 15:06
树形输出指的是按层次遍历么?是的话bfs不就OK了么