【C/C++】List Leaves/层序遍历叶子结点

本文提供了一道名为ListLeaves的编程题解答方案,该题要求按照从上到下及从左到右的顺序列出树的所有叶子节点。文章详细介绍了使用结构数组建立树并利用队列实现层序遍历来解决问题的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7-4 List Leaves(25 分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5
作者: 陈越
单位: 浙江大学
时间限制: 400ms
内存限制: 64MB
代码长度限

题目源于:https://pintia.cn/problem-sets/16/problems/666

题目要求:通过一个结构数组建立一棵树,按照从上到下,从左到右的顺序,打印所有叶子结点。

思路分析:通过判断结构数组的left,right是否为‘-’,来判断是否是叶子结点,通过队列来层序遍历二叉树。

// ListLeaves.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#define MAXSIZE 10
using namespace std;

//template<class T> typedef struct Queue *queue;

struct Node {
	int pos;
	char left;
	char right;
	Node() {
		pos = -1;
		left = -1;
		right = -1;
	}
};
typedef struct Node* node;


                               
struct Queue {
	struct Node data[MAXSIZE];

	int front;
	int rear;
};
typedef struct Queue* queue;
//程序主要操作
void input(struct Node * node, int N);
void process(int * a, struct Node * node,int N);
int find(struct Node * T,int N);
//层序遍历
int * levelOrder(struct Node * T, int root);
//队列基本操作
queue CreateQueue();
struct Node deleteQueue(queue Q);
void addQueue(queue Q,struct Node data);
bool isEmpty(queue Q);
bool isFull(queue Q);


int main()
{
	struct Node list[MAXSIZE];
	int N;
	//input
	cin >> N;
	input(list, N);
	//process
	int *a = new int[N];
	for (int i = 0; i < N; i++) a[i] = 0;
	//记录叶子结点
	process(a, list,N);
	//output
	int root = find(list, N);
	int sum=0;
	for (int i = 0; i < N; i++) { if (a[i] == 1) sum++; }
	//因为需要按照从上到下,从左到右的顺序打印,所以对对树进行层序遍历,按照遍历顺序依次打印叶子结点
	int *level = levelOrder(list, root);
	for (int i = 0; i < N && sum>1; i++) {
		if (a[level[i]] == 1) {
			cout << level[i] << ' ';
			sum--;
		}
	}
	for (int i = N - 1; i >= 0; i--) {
		if (a[level[i]] == 1) {
			cout << level[i];
			break;
		}
	}
	
	delete[] a;
    return 0;
}
//输入函数
void input(struct Node * list, int N) {
	for (int i = 0; i < N; i++) {
		list[i].pos = i;
		cin >> list[i].left >> list[i].right;
	}
}
//寻找叶子结点
void process(int * a, struct Node * node,int N) {
	for (int i = 0; i < N; i++) {
		if (node[i].left == '-'&&node[i].right == '-') a[i] = 1;
	}
}
//寻找树根
int find(struct Node * T, int N) {
	int *a = new int[N];
	for (int i = 0; i < N; i++) a[i] = 0;
	for (int i = 0; i < N; i++) {
		if (T[i].left != '-') {
			int l = T[i].left - '0';
			a[l] = 1;
		}
		if (T[i].right != '-') {
			int r = T[i].right - '0';
			a[r] = 1;
		}
	}
	int i;
	for ( i = 0; i < N; i++) if (!a[i]) break;
	delete[] a;
	return i;
}

int integer(char a) {
	return a - '0';
}

//创建一个空的队列
queue CreateQueue() {
	queue Q = new struct Queue;
	Q->front = -1;
	Q->rear = -1;

	return Q;
}

//出队
struct Node deleteQueue(queue Q) {
	if (isEmpty(Q)) {
		cout << "Queue is empty!" << endl;
		
	}
	else
	{
		Q->front = Q->front + 1;
		//Q->data[Q->front] ;
		return Q->data[Q->front];
	}
	
}
//入队
void addQueue(queue q, struct Node data) {
	if (isFull(q)) {
		cout << "Queue is full!" << endl;
		return;
	}
	else
	{
		q->rear = q->rear + 1;
		q->data[q->rear] = data;
	}
}
//判断队列是否为空
bool isEmpty(queue Q) {
	if (Q->front == Q->rear) return true;
	else return false;
}
//判断队列是否为满
bool isFull(queue Q) {
	if (Q->front == (Q->rear + 1) % MAXSIZE) return true;
	else return false;
}
//层序遍历
int* levelOrder(struct Node *T, int root) {
	int * a = new int[MAXSIZE];
	int i = 0;
	queue q;
	q = CreateQueue();
	//q->data[1] = T[root];
	addQueue(q, T[root]);
	while (!isEmpty(q)) {
		struct Node tmp = deleteQueue(q);
		a[i++] = tmp.pos;
		if (tmp.left != '-'&&tmp.left != '-1') {
			int l = tmp.left - '0';
			addQueue(q, T[l]);
		}
		if (tmp.right != '-'&&tmp.right != '-1') {
			int r = tmp.right - '0';
			addQueue(q, T[r]);
		}
	}
	return a;
}



非法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值