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;
}
【python数据挖掘课程】二十八.基于LDA和pyLDAvis的主题挖掘及可视化分析 这是《Python数据挖掘课程》系列文章,前面很多文章都讲解了数据挖掘、机器学习,这篇文章主要讲解LDA和pyLDAvis算法,同时讲解如何读取CSV文本内容进行主题挖掘及可视化展示。文章比较基础,希望对你有所帮助,提供些思路,也是自己教学的内容。推荐大家购买作者新书《Python网络数据爬取及分析从入门到精通(分析篇)》,如果文章中存在错误或不足之处,还请海涵。 目录: 一.显示结果及安装 二.LDA主题挖掘 三.pyLDAvis可视化分析 四.小结 阅读详情

相关推荐

泛微E9表单建模查询列表勾选后带入流程明细表中

注意:参照下图各项配置,否则会有问题。

泛微二次开发后端知识总结 5309

详解-03树2 List Leaves

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

Young824的博客 880

SSTI模板注入及绕过姿势(基于Python-Jinja2)

http://xmctf.top:8962/?name={{%22%22[%22\x5f\x5fclass\x5f\x5f%22]["\x5F\x5Fbase\x5F\x5F"]["\x5F\x5Fsubclasses\x5F\x5F"]()[233]["\x5F\x5Finit\x5F\x5F"]["\x5F\x5Fglobals\x5F\x5F"]["\x5F\x5Fbuiltins\x5F\x5F"]['eval']("\x5F\x5Fimport\x5F\x5F('os'))")}}

Y4tacker的博客 1万+

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

PDMS二次开发入门:手把手教你用PML2写第一个交互式窗体工具

本文详细介绍了如何使用PML2进行AVEVA PDMS二次开发,从零开始构建交互式窗体工具。通过PML2语言的面向对象特性、即时调试能力和深度系统集成优势,开发者可以高效创建自动化设计工具、批量修改属性和定制报表系统。文章包含开发环境配置、基础语法、窗体架构设计、事件处理等实战内容,帮助读者快速掌握PDMS二次开发技能。

weixin_29281941的博客 271

Ansys Lumerical 启动时出现 Error: Could not connect to Ansys license server specified at 1055@ 的一种解决办法

考虑到 2020 版的时候是直接在全局变量里指定具体地址,但是 2024 版本是写成 1055@localhost ,并且之后在站里搜相关 Ansys 的问题都指向 1055 端口被占用,虽然问题不一样,但是本着死马当活马医,就按照。我最近在跑超透镜结构,听说2024版本现在有GPU加速可以用,就把Lumerical从2020升到2024版本,结果发现一开头的破解办法完全不一样了。一开始安装的时候以为2020没有删干净,导致2024版的安装一直锁着目标地址,解决办法参考。的办法来操作,在关闭了。

qq_51404184的博客 5753

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

ESP32-P4 GPIO双域架构:HP/LP分离、寄存器级控制与低功耗工程实践

GPIO(通用输入输出)是嵌入式系统中最基础且高频使用的硬件接口,其性能、灵活性与功耗特性直接影响产品实时性与续航能力。现代高性能MCU如ESP32-P4通过HP/LP双电源域分离、可编程IO复用(IO MUX)、信号交换矩阵及硬件自动化机制(如ETM事件、Sigma-Delta调制),将传统GPIO升级为兼具模拟能力、亚微秒响应与微安级待机的智能外设子系统。其核心价值在于实现引脚功能动态重构、中断零延迟处理、Deep-sleep期间自主唤醒与多核安全操作。典型应用场景涵盖工业PLC信号采集、电池供电传感器

weixin_35949153的博客 96

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

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

m0_72805195的博客 1248

Qt Quick (QML) 老年人健康管理

通过 **Qt SQL** 模块将所有健康数据(用药记录、体检数据等)安全地存储在本地 **SQLite** 数据库中。你想用 **Qt C++** 结合 **Qt Quick** (QML) 来开发一个老年人健康管理软件,主要功能包含 **用药提醒** 和 **健康记录** (使用 `Qt SQL` 存储数据)。* **UI 框架**: Qt Quick (QML) - 用于构建流畅、动态的用户界面。**数据模型类 (models/MedicationReminder.h / .cpp)**

寒寒豆豆软件公司的博客 193

RT-DETR算法优化改进:DCNv4更快收敛、更高速度、更高性能,效果秒杀DCNv3、DCNv2等 ,助力检测

DCNv4更快收敛、更高速度、更高性能,完美和RT-DETR结合,助力涨点

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 1323

[浙大数据结构] 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

超简单五步实现VOS3000客户端呼入配置

1.查看SIP代理  在注册管理中查看自己线路注册的SIP代理IP地址,记录下来。 2.创建普通账户  创建一个呼入使用的普通账号,选择“计费费率”,根据自己情况选择费率,这里做测试就配置了免费的费率,如果费率不是免费,需要给这个普通账户充值。 3.新建对接网关 新建一个对接网关,点击该网关的“补充设置”,选择“网关类型”为静态IP,然后在IP地址栏填上刚才在注册管理里面记录的SIP代...

非著名运维的博客 1万+

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

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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、付费专栏及课程。

余额充值