List Leaves树的层序遍历

《数据结构》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 a... 阅读详情

mid-14. List Leaves (15 分)

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

思路: 根结点肯定不是其他节点的孩子,所以样例中未出现的3就是根结点。构造出二叉树,并使用队列层序输出。采用C++队列操作。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<queue>
using namespace std;
queue<int> q;


struct TreeNode
{
	int left;
	int right;
}T[10];
int check[10];//开一个标记数组


int Buildtree(struct TreeNode T[])
{
	int n,i,Root=-1;
	char cl,cr;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("\n%c %c",&cl,&cr); 
		if(cl!='-')//非空就存入
		{
			T[i].left =cl-'0';//字符转化为数字 
			check[T[i].left ]=1;/标记已存入
		}
		else T[i].left =-1;
		if(cr!='-')
		{
			T[i].right =cr-'0';
			check[T[i].right ]=1;
		}
		else T[i].right =-1;
	}
	for(i=0;i<n;i++)
	{
		if(check[i]==0)//遇到头节点就退出 
		break;
	}
	Root=i;//未被存入的就是头节点 
	return Root;
}

void Output_leaves(int Root)
{
	int count=0;
	int temp;
	if(Root==-1)return;
	q.push(Root);
	while(!q.empty())
	{
	temp=q.front();//队头给temp
	q.pop();//出队 
	if(T[temp].left ==-1&&T[temp].right ==-1)//若是叶子节点,则输出
	{
		if(count++!=0)
		{
			printf(" ");//输出空格用的 
		}
		printf("%d",temp);
	}
	if(T[temp].left !=-1)q.push(T[temp].left);//左孩子进队 
	if(T[temp].right !=-1)q.push(T[temp].right);//右孩子进队 
    }
}                                          


int main()
{
	int Root;
	Root=Buildtree(T);
	Output_leaves(Root);
	return 0;
} 

 

C# 【WebService编写调用】 😹 觉得博主文章写的不错的话,希望大家三连(✌关注,✌点赞,✌评论),多多支持一下!!! 阅读详情

相关推荐

K-L展开 (Karhunen-Loeve expansion)

K-L变换

susan_wang1的博客 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 positive integer N (≤10) which is the total number of no.

sankuaizhuobu的博客 319

5分钟搞定:用Ollama在MacBook上本地运行Qwen3大模型(附避坑指南)

本文详细介绍了如何在MacBook上使用Ollama快速部署和运行Qwen3大模型,包括安装步骤、性能优化技巧及常见问题解决方案。Ollama作为一款轻量级工具,特别适合M系列芯片的Mac用户,支持多种量化版本,显著降低资源消耗,提升推理速度。

sql99的博客 824

List Leaves 树的层序遍历

3-树2List 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 ea...

weixin_30300225的博客 107

基于 Quest 摄像头数据开发的原理介绍【Unity Meta Quest MR 开发教程】

比如想在识别到的现实物体上叠加虚拟物体,需要知道这个现实物体在 Unity 的世界坐标下的哪个位置,是带有 x,y,z 三个轴的数据的。而 WebCamTexture 捕捉到的图像只是个 2D 的平面,没有三维世界的深度信息,我们通过物体识别算法识别到的物体坐标只是在这个 2D 平面上的一个屏幕坐标,可以理解为 Quest 拍摄了一张照片,我们识别到这张照片上的某个像素点为需要识别的现实物体的中心。是不是看起来比较复杂?Quest 的相机访问功能是在安卓的。

qq_46044366的博客 2105

银河麒麟v10离线安装docker二进制包

因为工作需要在银河麒麟v10系统上离线部署k8s集群,k8s部署的前提是具有docker环境,因此离线安装docker成为必然。 1.下载docker包:Index of linux/static/stable/x86_64/ 选择自己合适的版本,我下载的是20.10.7 选择自己放置docker包的路径进行解压: cd /home/huawei/zq tar -zxvf docker-20.10.7.tgz 2. 给docker包解压出来的docker文件夹下的二进制文件赋..

q_hsolucky的博客 7686

03-树2 List Leaves (25 分)(树的层序遍历queue及bfs)

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

小牛的博客 266

医学成像学习笔记(一):核磁共振成像(MRI)k空间为何是图像频谱详解

前言   k空间是核磁共振成像图像重建的核心,可能很多人像笔者一样第一次学时会非常疑惑,为何k空间是图像的频域空间,观其填充过程,明明是空域信号的采样填充呀。网上很少有文章讨论这个问题,所以笔者在此写下自己的理解,供大家参考。 MRI位置编码   现在的MRI一次扫描的是一个断层或者多个断层,所谓断层就是一个有厚度的面,通过z方向梯度场来选择。我们扫描一个断层的目的是为了获得该断层各位置的质子情况,它们携带着组织的性质信息,反应出来的就是信号强度。如果整个断层都采用相同的磁场强度,而我们只能得到一个断

意疏的学习笔记 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. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is

_计算机好苦 220

ue 打包linux程序 在windows系统 2026

ue 打包linux程序 在windows系统 2026

jacke121的专栏 372

15、运行语音识别应用:从Mac到微控制器的部署指南

本文详细介绍了如何在Mac上运行语音识别应用,并将其部署到Arduino Nano 33 BLE Sense和SparkFun Edge等微控制器设备上的完整流程。内容涵盖应用构建、设备特定的音频与输出实现、部署步骤、常见问题解决方案及进一步优化建议。通过本指南,开发者可快速实现基于TensorFlow Lite的嵌入式语音识别系统,并根据需求进行自定义修改与功能扩展。

l3m4n的博客 59

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.Input Specification:Each input file contains one test case. For each case, the ...

ACMer的知识记录 487

Android12 适配 usb 触屏

在 Android 系统中,输入设备 IDC(Input Device Configuration)设备描述文件(.idc)是用于配置和定义输入设备行为和属性的文本文件。 该文件通常在`/system/usr/idc/` 或 `/vendor/usr/idc/`,具体位置因芯片厂商SDK而异。一般情况此类文件是预置到系统,开发者 root 权限才能修改这些文件。输入设备 IDC 设备描述文件在 Android 系统中起到了识别、配置、映射和定制输入设备的作用,使得系统能够准确解析和处理各种输入设备的事件,提

weixin_35723192的博客 1234

03-树2 List Leaves输出叶子结点,二叉树的层序遍历

03-树2 List Leaves输出叶子结点,二叉树的层序遍历输出叶子结点,二叉树的层序遍历题目出处输入描述Input Specification:输出描述Output Specification:输入样例Sample Input:输出样例Sample Output:C语言代码实现 输出叶子结点,二叉树的层序遍历 题目出处 题目出处:中国大学MOOC-陈越、何钦铭-数据结构 输入描述Input ...

CHINANET 518

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

PTA 03-树2 List Leaves(数组模拟链式存储+层序遍历叶子节点)

大体思路: 1.选取数据结构,输入的是每个0到N-1节点的左右孩子情况,考虑数组模拟链式存储,然后根据度数找出根(度数为零)。 代码: struct treeNode//数组模拟链式存储 { int left, right; }d[1005]; cin>>n; //读入 for(int i=0;i<n;++i) { ...

weixin_30809333的博客 144

廖雪峰Python教程[完整版]

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

03-树2. List Leaves (25) 二叉树的层序遍历

03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912 Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specific...

weixin_30865427的博客 120

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

03-树2 List Leaves(C++实现):二叉树建立+层序遍历+叶节点+#include <queue> 的使用

原题目 03-树2List 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...

进一寸有一寸的欢喜 385

PAT甲级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 ...

qq_40059727的博客 230

算法 树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

03-树2 List Leaves

03-树2 List Leaves

Chen 1275

PTA 03-树2 List Leaves

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

2301_80635769的博客 1505

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

CoderL 2889

【树】List Leaves

【树】List Leaves 题目要求: 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 gives a positive integer N (≤10) which is the total numbe

百栗.的博客 310

List Leaves

03-树2 List Leaves原题链接解题思路代码实现 原题链接 树(上)课后练习题2 解题思路 又是需要自己找根结点的问题(和之前的思路一样,不是任何结点的孩子那它就是根结点),利用了静态链表的思想,创建结构体数组在读入数据的时候记录每个结点的左右孩子的地址(用数组下标)。 然后进行层序遍历,用STL中的队列queue实现非常方便,这里要求只输出叶子结点,那只要在访问结点(这里访问就是输出其...

qq_40486952的博客 254

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

[PTA刷题]树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 give

黑客与画家 449
上一篇: 欧拉图(路径)与哈密顿图(通路)
下一篇: 股票涨了吗(有点KMP的思想)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值