03-树2 List Leaves(浙大数据结构PTA习题)

03-树2 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 N−1. 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

代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB

题目解析:

题目大意:给定一系列树的结点,按照从上到下,从左到右的顺序输出这棵树的叶子结点

关键点1:找到树的根结点

关键点2:根据树的根结点以及其它一系列结点构建树

采用递归的思路,如果根结点的左子树不为空,则递归构建左子树,否则为NULL;如果根结点的右子树不为空,则递归构建右子树,否则为NULL;

关键点3:按照从上到下,从左到右的顺序输出这棵树的叶子结点

本质是树的层序遍历问题,借助队列实现;

参考代码:

cpp 复制代码
# include<stdio.h>
# include<stdbool.h>
# include<stdlib.h>
# define MAXNODE 10 

typedef char ElementType;

typedef struct TreeNode* Tree;
struct TreeNode{
	ElementType info,left,right;
	Tree Left;
	Tree Right;
};

Tree Create(); 
Tree InitialTree(Tree Array[],int N);
Tree CreateTree(Tree Array[],Tree Root);
void InOrderPrint(Tree tree);

int main(){
	// 根据输入创建树 
	Tree tree = Create();
	// 对这棵树进行层序遍历,并输出叶子结点
	InOrderPrint(tree); 
	return 0;
}

// 对一棵树进行层序遍历,并输出叶子结点
void InOrderPrint(Tree tree){
	// 创建一个结点指针队列	
	Tree Queue[MAXNODE];
	int Rear=-1, Head = -1;
	// 压入根结点
	Queue[++Rear] = tree;
	// 用于格式化输出的统计
	int count = 0; 
	while(Rear!=Head){
		// 从队列出队一个结点,并判读是否是叶节点 
		Tree tmp = Queue[++Head];
		if(tmp->Left == NULL && tmp->Right==NULL){
			if(count==0){
				printf("%d",tmp->info-'0');
				count++;
			}
			else printf(" %d",tmp->info-'0');
		}
		// 分别压入其左右结点(若不为空)
		if(tmp->Left)Queue[++Rear] = tmp->Left;
		if(tmp->Right)Queue[++Rear] = tmp->Right; 
	} 
	return;
	 
} 
 

// 根据输入创建一棵树
Tree Create(){
	int N;
	scanf("%d",&N);
	getchar();
    if(N==0)return NULL;
	Tree Array[N];
	// 将信息存入指针数组中并获得树的根结点
	Tree Root = InitialTree(Array,N);
	// 通过树的根结点来建立树 
	Tree tree = CreateTree(Array,Root);
	return tree; 
} 

// 将结点信息存入指针数组中,并返回树的根结点 
Tree InitialTree(Tree Array[],int N){
	// Check数组用来标记结点是否作为了子节点 
	int i,Check[N];
	for(i=0;i<N;i++)Check[i] = 1;
	// 读入结点信息,并判读每个结点是否作为了子节点 
	for(i=0;i<N;i++){
		Tree node = (Tree)malloc(sizeof(struct TreeNode));
		node->info = '0'+i;
		node->left = getchar();
		if(node->left!='-')Check[node->left-'0'] = 0;
		getchar();
		node->right = getchar();
		if(node->right!='-')Check[node->right-'0'] = 0;
		getchar();
		node->Left = node->Right = NULL;
		Array[i] = node;
	}
	for(i=0;i<N;i++){
		if(Check[i]==1)break;
	}
	return Array[i];
}

// 根据指针数组及根结点递归构建一棵树
Tree CreateTree(Tree Array[],Tree Root){
	int i,count;
	// 递归构建左子树 
	if(Root->left == '-'){
		Root->Left = NULL;
	}else{
		Root->Left = CreateTree(Array,Array[Root->left-'0']);
	}
	// 递归构建右子树
	if(Root->right == '-'){
		Root->Right = NULL;
	}else{
		Root->Right = CreateTree(Array,Array[Root->right-'0']);
	}
	return Root;
} 

运行结果:

相关推荐
superior tigre27 分钟前
C++学习:六个月从基础到就业——C++11/14:其他语言特性
c++·学习
天堂的恶魔94641 分钟前
C++ - 仿 RabbitMQ 实现消息队列(2)(Protobuf 和 Muduo 初识)
c++·rabbitmq·ruby
休息一下接着来1 小时前
进程间通信(IPC)常用方式对比
linux·c++·进程间通讯
虾球xz1 小时前
游戏引擎学习第288天:继续完成Brains
c++·学习·游戏引擎
John_ToDebug1 小时前
Chromium 浏览器核心生命周期剖析:从 BrowserProcess 全局管理到 Browser 窗口实例
c++·chrome·性能优化
爱coding的橙子1 小时前
每日算法刷题Day11 5.20:leetcode不定长滑动窗口求最长/最大6道题,结束不定长滑动窗口求最长/最大,用时1h20min
算法·leetcode·职场和发展
WenGyyyL1 小时前
力扣热题——零数组变换 |
算法·leetcode·职场和发展·蓝桥杯
芯眼1 小时前
AMD Vivado™ 设计套件生成加密比特流和加密密钥
算法·fpga开发·集成测试·软件工程
咪嗷喵挖藕哇1 小时前
leetcode 合并区间 java
java·算法·leetcode
沐风ya1 小时前
leetcode每日一题 -- 3355. 零数组变换 I
算法·leetcode