欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基础实验4-2.2-列出叶结点-函数题

程序员文章站 2022-03-13 15:56:42
...

基础实验4-2.2-列出叶结点-函数题

解题代码

#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;
}

测试结果

基础实验4-2.2-列出叶结点-函数题

问题整理

1.这题嗨星。