树2 List Leaves

英伟达NVIDIA数字IC后端笔试真题(ASIC Physical Design Engineer) 英伟达NVIDIA数字IC后端笔试真题(ASIC Physical Design方向) 阅读详情

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 (≤) 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

#include <stdio.h>

#define Null -1

struct TreeNode
{
	char Element;
	int Left;
	int Right;
}Tree[10];

int ReadTree(struct TreeNode Tree[]);
void ListLeaf(int Root);

int main(){
	int Root;
	Root = ReadTree(Tree);  //读入树
	ListLeaf(Root); //输出叶子
	
	return 0;
}

int ReadTree(struct TreeNode Tree[]){
	int N,i;
	char cl,cr;
	scanf("%d",&N);
	if(N){
		int Flag[10]={0};
		for (i = 0; i < N; ++i)
		{
			scanf("%c %c %c",&Tree[i].Element,&cl,&cr);
			if(cl!='-'){
				Tree[i].Left = cl - '0';
				Flag[Tree[i].Left]=1;
			}
			else Tree[i].Left = Null;
			if(cr!='-'){
				Tree[i].Right = cr - '0';
				Flag[Tree[i].Right]=1;
			}
			else Tree[i].Right = Null;			
		}
		for (i = 0; i < N; ++i)
		{
			if(!Flag[i]) break;/* code */
		}
		return i;
	   //判断根节点

	}
	else
		return Null;
}

//需要层序遍历
void ListLeaf(int Root){
	int head=0,tail=0,A[10]={0},flag=0;
	A[tail++] = Root;
	while( head < tail ){                            //层序遍历
		if(Tree[A[head]].Left != Null) 
			A[tail++] =  Tree[A[head]].Left;  //左节点入队
		if(Tree[A[head]].Right != Null) 
			A[tail++] =  Tree[A[head]].Right;
		
		if((Tree[A[head]].Left == Null) &&
			(Tree[A[head]].Right == Null) ) {
			if(flag) printf(" %d",A[head]);
			else printf("%d",A[head]);
			flag++;
		}
		head++;                      //相当于根节点出队
	}

}

这个题目差不多是自己独立完成的,开心~

Reflection:

    1.利用数组实现一个简单的队列

    2.层序遍历

参考资料:https://www.cnblogs.com/8023spz/p/7748203.html

【示波器专题】示波器探头的原理深入——有源探头之单端有源探头 由于接地环路的苛刻要求,更高带宽的单端有源探头就算设计了也不实用。因此单端有源探头的带宽一般在6GHz以内,更高带宽的信号测量也只能使用差分有源探头(市面上有些型号的差分探头可同时兼顾单端测试)。...... 阅读详情

相关推荐

汽车工程师必看:从CAN到Ethernet,6种车载通信协议全解析(附应用场景对比)

本文全面解析了从CAN到Ethernet的6种车载通信协议,包括CAN、LIN、MOST、FlexRay等,并对比了它们的应用场景和技术特点。文章详细介绍了各协议的演进历程、技术优势及在现代汽车中的混合网络架构设计,为汽车工程师提供了实用的协议选型指南和未来通信技术发展趋势。

weixin_28683689的博客 493

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

Fencingworld的专栏 492

STM32F103C8T6+A4988驱动42步进电机全流程(附接线图+代码解析)

本文详细介绍了使用STM32F103C8T6单片机与A4988驱动模块控制42步进电机的完整流程。内容涵盖硬件接线图解析、电流设置、GPIO驱动代码编写以及细分控制等核心实践,旨在帮助嵌入式开发者和DIY爱好者快速实现精准运动控制,适用于3D打印机、CNC等需要精密定位的项目。

Python的专栏 796

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

高通量测序数据分析:RNA-seq

深度测序相关数据库与数据格式 SRA toolkit 一、NCBI 和EBI、DDBJ组成INSDC,数据内容相同所以找NCBI就行。 (一)NCBI常用数据库 GenBank:遗传序列数据库,收集了所有公开的DNA序列及其注释 GEO (Gene Expression Omnibus) :收集整理各种表达芯片数据,后来加入了甲基化、lncRNA、miRNA、CNV等其他芯片,还有高通量测序数据...

qq_41134363的博客 3万+

VS2019+EasyX图形库实战:手把手教你用C语言打造消消乐小游戏(附完整源码)

本文详细介绍了如何使用VS2019和EasyX图形库从零开始构建一个C语言消消乐小游戏。通过环境配置、数据结构设计、界面绘制、交互实现到游戏逻辑完善的完整流程,帮助开发者掌握图形编程和游戏开发的核心技巧,并附有完整源码供参考。

bush的博客 186

(2016秋数据结构课后练习题总结)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

时间杀手Time的博客 3810

台球 PCOL:极致物理还原的网页斯诺克引擎(附源码深度解析)

*完整源码地址**:[https://github.com/billiards/pcol-core](https://github.com/billiards/pcol-core)(包含详细文档与示例)通过这个项目,我们不仅创建了一个游戏,更构建了一个**可扩展的物理引擎框架**,为网页端复杂物理模拟开辟了新的可能性。**在线体验**:[https://pcol.billiards](https://pcol.billiards)(支持移动端触控操作)clone.velocity.x *= -0.92;

Loving_enjoy的博客 2803

算法 树2 List Leaves

题目: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...

Dezeming的博客 6551

Palantir如何利用Ontology构建企业级数据地图

Palantir通过"本体论"(Ontology)技术构建动态数据地图,将多源异构数据转化为可操作的业务知识网络。该技术突破传统ETL模式,建立覆盖全组织的语义层,将原始数据映射为"客户""订单"等业务对象及其关联关系,形成可推理的语义网络。核心Foundry平台提供本体管理工具(OMA)和开发套件(OSDK),支持从数据连接到应用开发的全流程。虽然采用专有实现而非标准本体语言,但该架构成功解决了数据孤岛问题,赋能复杂决策分析

Learning 1804

汽车行业新能源汽车数据爬虫:抓取新能源汽车数据,分析市场发展和技术创新

在汽车行业,新能源汽车市场的发展和技术创新是当前的热点话题。通过抓取新能源汽车的数据,我们可以深入了解市场趋势、消费者需求以及技术创新的方向。本文将详细介绍如何使用 Python 爬虫技术抓取新能源汽车数据,并对数据进行分析和可视化展示。

u014481728的博客 1144

03-树2 List Leaves

03-树2 List Leaves

Chen 1275

Langchain cannot connect with Azure SQL Database

Langchain 无法连接到 Azure SQL 数据库

suiusoar 633

PTA 03-树2 List Leaves

2、如何遍历树,并按要求输出叶子节点:由于题目要求是从上到下,从左到右,这意味着在遍历当前层的节点之前,需要先遍历完上一层的节点,这显然是层序遍历。层序遍历基于队列的数据结构实现,这里我使用的是数组模拟队列。大致意思就是:输入节点个数,然后按顺序(0到N-1)输入每个节点的左右子树的索引,要求输出叶子节点,顺序为从上到下,从左到右。1、创建树:由于是二叉树,我们可以选择用结构体数组去存树的节点以及左右孩子的索引。注意:这里对输出有要求,所以先将符合要求的节点放入vector中,最后一起输出。

2301_80635769的博客 1505

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

详解-03树2 List Leaves

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

Young824的博客 880

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

树2——List Leaves

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

Beyond the Sora 614

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

C语言 03-树2 List Leaves

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

m0_37407587的博客 764
上一篇: 树1 树的同构
下一篇: 树3 Tree Traversals Again
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值