基础实验4-2.2-列出叶结点-函数题
程序员文章站
2022-03-13 15:56:42
...
解题代码
#include<stdio.h>
#include<stdlib.h>
typedef struct queue que;
struct queue {
int left;
int right;
};
int main()
{
int N, i;
scanf("%d", &N);
int * check = (int *)malloc(N * sizeof(int));
for (i = 0; i < N; i++) check[i] = 0;
que* a = (que*)malloc(N * sizeof(struct queue));
char tl, tr, temp;
for (i = 0; i < N; i++) {
temp = getchar();
scanf("%c %c", &tl, &tr);
if (tl == '-') a[i].left = -1;
else {
a[i].left = tl - '0';
check[a[i].left] = 1;
}
if (tr == '-') a[i].right = -1;
else {
a[i].right = tr - '0';
check[a[i].right] = 1;
}
}
for (i = 0; i < N; i++) if (!check[i]) break;
int r = i;
int* q = (int *)malloc(N * sizeof(int));
int front = 0, rear = 0;
q[rear++] = r;
int flag = 1;
while (front != rear) {
int t = rear;
if (a[q[front]].left != -1) q[rear++] = a[q[front]].left;
if (a[q[front]].right != -1) q[rear++] = a[q[front]].right;
else if(a[q[front]].left == -1){
if (flag) flag = 0;
else printf(" ");
printf("%d",q[front]);
}
front++;
}
return 0;
}
测试结果
问题整理
1.这题嗨星。
上一篇: 进阶实验2-3.1-海盗分赃-编程题
下一篇: 基础实验3-2.5-堆栈模拟队列-函数题