03-树2 List Leaves(25 分)

03-树2 List Leaves (25分) 03-树2 List Leaves (25分) 思路分析:本体要自己创建一个二叉树,然后按照层序输出叶节点,一说到层序我们就会想到层序遍历,层序遍历我们常用的方法无非是队列,从根节点开始进入队列,设置一个临时变量,判断队列是否为空,然后获取队列头元素,然后出栈,并且输入它,判断这个临时变量是否有左右孩子如果有,则依次将左孩子,右孩子进入队列,依次类推反复执行。 这道题要用到queue的库函数,就用C++来实现的,queue容器提供了以下的操作: front():返回 queue 中第一个元素的引用。如果 阅读详情

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)N (≤10)N(≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 000 to N−1N−1N−1. Then NNN 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

#include<iostream>
#include<cstdlib>
#define Tree int
#define MaxSize 10
#define Null -1

using namespace std;

//定义树结构
struct TreeNode {
	Tree Left;
	Tree Right;
}T[MaxSize];

//定义队列结构
typedef struct QueueNode {
	int Data;
	struct QueueNode *Next;
}QueueNode, *Queue;

//初始化队列
Queue initQueue(void) {
	Queue q = (Queue)malloc(sizeof(QueueNode));
	q->Next = NULL;
	return q;
}

//判断队列是否为空
int isQueueEmpty(Queue Q) {
	return Q->Next == NULL ? 1 : 0;
}

//入队
void enQueue(int data, Queue Q) {
	Queue p = initQueue(), q = Q;
	p->Data = data;
	while (!isQueueEmpty(q)) q = q->Next;
	q->Next = p;
}

//出队
int deQueue(Queue Q) {
	if (isQueueEmpty(Q)) return NULL;
	Queue q = Q, p;
	int result;
	p = Q->Next;
	q = p->Next;
	result = p->Data;
	Q->Next = q;
	free(p);
	return result;
}

//输入数据并返回该数根节点的位置
Tree BuildTree(struct TreeNode T[]) {
	int n, check[MaxSize], i, root = Null;
	char tempLeft, tempRight;
	scanf("%d", &n);
	if (n) {
		for (i = 0; i < n; i++) check[i] = 0;
		for (i = 0; i < n; i++) {
			//接收上一次输入后残留的\n,防止对后面的输入产生影响
			tempLeft = getchar();
			scanf("%c %c", &tempLeft, &tempRight);
			if (tempLeft != '-') {
				T[i].Left = tempLeft - '0';
				check[T[i].Left] = 1;
			}
			else T[i].Left = Null;
			if (tempRight != '-') {
				T[i].Right = tempRight - '0';
				check[T[i].Right] = 1;
			}
			else T[i].Right = Null;
		}
		for (i = 0; i < n; i++) if (check[i] == 0) break;
		root = i;
	}
	return root;
}

//找到所有叶子结点,并按照从上至下、从左至右的顺序输出它们的索引
void FindLeaves(Tree R) {
	if (R == Null) return;
	bool isFirst = true;
	Tree temp = R;
	Queue Q = initQueue();
	enQueue(R, Q);
	while (!isQueueEmpty(Q)) {
		temp = deQueue(Q);
		if (T[temp].Left == Null && T[temp].Right == Null) {
			if (isFirst) {
				cout << temp;
				isFirst = false;
			}
			else cout << ' ' << temp;
		}
		if (T[temp].Left != Null) enQueue(T[temp].Left, Q);
		if (T[temp].Right != Null) enQueue(T[temp].Right, Q);
	}
	cout << endl;
}

int main(void) {
	Tree R;
	R = BuildTree(T);
	FindLeaves(R);
	return 0;
}
【unity】【WebRTC】从0开始创建一个Unity远程媒体流app-构建可同步场景 本文介绍了如何利用Unity和WebRTC技术实现远程画面传输。通过配置Render Streaming组件、设置信令管理器和视频流发送器,构建了一个可同步的Unity场景。文中详细说明了从场景搭建到浏览器端测试的完整流程,并提供了常见问题排查方法。该方案适用于远程调试、云游戏、VR共享等场景,具有低延迟、高兼容性等特点。文章还包含项目源码下载链接,方便读者实践操作。最终实现了Unity端到浏览器端的实时画面传输,为后续实现双向互动奠定了基础。 阅读详情

相关推荐

用Keil5给CH552G编程:手把手配置8051开发环境+LED闪烁实战

本文详细介绍了如何使用Keil5为CH552G单片机配置8051开发环境,并完成LED闪烁实战。从获取必要软件、安装WCH器件库到创建工程、编写程序及烧录,手把手指导开发者避开常见陷阱,特别适合从STC转向CH552G的开发者。

weixin_30733003的博客 539

详解-03树2 List Leaves

题目来源: 中国大学MOOC-陈越、何钦铭-数据结构 题目详情: 译文: 给定一棵树,按照从上到下,从左到右的顺序列出所有叶结点。 输入格式: 每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数N(≤10)N(≤10)N(≤10),它是树中节点的总数。节点从000到N−1N-1N−1编号。 然后跟随NNN行,每行对应一个节点,并给出该节点左右子节点的索引。 如果孩子不存在,将在该位置放置一个“-”。 任何一对孩子都用空格隔开。 输出格式: 对于每个测试用例,按从上到下,从左到右的顺序在一行

Young824的博客 880

深入解析Panfrost驱动:Mali-GPU开源驱动的架构与实现

本文深入解析了Panfrost驱动作为Mali-GPU开源驱动的架构与实现。从诞生背景到硬件架构演进,详细介绍了Panfrost如何突破ARM闭源策略,实现全栈支持。文章还探讨了驱动设计中的关键技术挑战与性能优化策略,为开发者提供了实用的编译部署指南和性能调优方法。

weixin_30790841的博客 552

03-树2 List Leaves

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. 大意:给你一棵树,你需要按照从上到下,从左到右的顺序输出它的叶子节点(度为0的节点) Input Specification: Each inp...

听维也纳的海风 362

离散小波变换(概念与应用)

离散小波变换(概念与应用)

weixin_46713695的博客 4415

Android 13.0 双屏时触摸失效问题排查与修复

摘要:本文记录了RK3588 Android12开发板上主屏触摸失效问题的排查与解决过程。通过分析发现,USB触摸设备被错误关联到辅屏(displayId=3)而非主屏(displayId=0)。解决方案是修改TouchInputMapper.cpp,强制触摸设备使用INTERNAL viewport(主屏),并保持设备在viewport不活跃时仍启用。该修改涉及约40行代码,有效解决了多显示器环境下触摸设备错误映射的问题,同时提供了详细的调试方法和注意事项。

ip0417的博客 353

03-树2 List Leaves(C/C++)

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. Fo...

叶柖的博客 911

java实现长短期记忆人工神经网络LSTM(附完整源码)

java实现长短期记忆人工神经网络LSTM(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1691

浙大数据结构第三周之03-树2 List Leaves

浙大数据结构第三周之03-树2 List Leaves

m0_72805195的博客 1248

JavaScript教程-字符集,Unicode转义序列,标识符,数据类型,数字,未定义值,浮点数,字符串,引用数据类型,类型检测,类型转换,运算符,关系运算符,

前端学习

VAN 212

零基础做完YOLOv8船只检测系统:HRSC2016数据集 + 完整UI,稳过答辩

亲爱的同学,通过这篇教程,你已经掌握了“基于YOLOv8的HRSC2016微小船只检测系统”从数据集处理、模型训练到Tkinter界面开发的全流程。这个课题既贴合“海事智能化”的产业战略,又涵盖了“目标检测、深度学习、界面开发”等多个毕设评分点,非常适合作为计算机或海事工程专业的毕业设计。在实践中,你可能会遇到“海面背景复杂导致船只漏检”“微小船只识别精度不足”“UI与模型推理同步卡顿”等挑战——别担心,这些都是技术成长的必经之路。

FJN110的博客 108

[浙大数据结构] 03-树2 List Leaves (25分)

1 题目描述 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 ...

努力变得不菜的菜鸡的博客 1401

UG NX二次开发(C++)-库缺少需要的入口点的原因与解决方案

在UG NX二次开发过程中,有时会遇到形形色色的bug,比如有个读者其采用UG NX二次开发(C++)时,遇到“库缺少需要的入口点”这个问题,看了一下他发给我的源代码,感觉有必要讲一下UG NX二次开发(C++)遇到此类问题的可能情况与解决方法。自然,这是博主的经验之谈,有不足之处还请在评论区讨论。

GimiGimmy 2201

03-树2 List Leaves (25分)

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,

david_liang 2507

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

03-树2 List Leaves (25 分

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, ...

weixin_38732825的博客 170

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

03-树2 List Leaves (25 分)

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. 输入格式: Each input file contains one test case. For each case, the first line ...

CO₂的博客 411

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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 posi...

qq_29718605的博客 589

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 ...

听维也纳的海风 233

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 giv...

diaohong5075的博客 78
上一篇: 03-树1 树的同构(25 分)
下一篇: 03-树3 Tree Traversals Again(25 分)
centralunit
博客等级 码龄10年 219粉丝 115原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值