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 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
解题思路:就是给你一颗树,让你按从上到下,从左往右的顺序输出叶子结点的序号;
代码如下:
#include<iostream>
using namespace std; int n ;
bool vis[];
struct node{
int lchild; //定义一个左孩子
int rchild; //定义一个右孩子;
}tree[]; //定义一个树的结构体;
int sz[]; //后面用来存树的信息;
int head = , rear = ; //后面方便输出叶子结点的序号;
int main()
{
cin>>n;
char l , r;
for(int i = ; i < n ;i++)
{
cin>>l>>r;
if(l!='-') //如果为数字;
{
tree[i].lchild = l - ''; //将其转化为数字;因为我们输入的是字符
vis[tree[i].lchild] = ; //并标记这个数字我们已访问过 ,是i的左的孩子;
}else
{
tree[i].lchild = -; //否则i没有左孩子,将其置为-1;
} if(r!='-') //为右孩子,原理同上;
{
tree[i].rchild = r - '';
vis[tree[i].rchild] = ;
}else
{
tree[i].rchild = -;
}
}
int root;
for(int i = ; i < n ;i++)
{
if(vis[i]==) //如果i都不是谁的孩子,没有被访问过,则它是根结点;
{
root = i ;
break;
}
}
int leaves = ;
sz[rear++] = root;
while(rear - head > )
{
int num = sz[head++];
if (tree[num].lchild == - && tree[num].rchild == -) { //输出叶节点,既没左孩子也没右孩子; if (leaves) printf(" "); //输出格式; printf("%d", num); ++leaves; //若是叶子结点,则叶子结点数目++; } if (tree[num].lchild != -) { //如果存在,左孩子入队 sz[rear++] = tree[num].lchild; } if (tree[num].rchild != -) { //如果存在,右孩子入队 sz[rear++] = tree[num].rchild; } }
return ;
}
PTA数据结构之 List Leaves的更多相关文章
- PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)
PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分) 7-43 字符串关键字的散列映射 (25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义 ...
- PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分)
PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分) 7-42 整型关键字的散列映射 (25 分) 给定一系列整型关键字和素数P,用除留余数法定义的散列函数将关键字映射 ...
- PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)
PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分) 计算机程序设计能力考试(Programming Ability Test,简称P ...
- PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)
PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分) 每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...
- PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)
PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分) 在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...
- PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)
PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...
- PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)
PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分) Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...
- PTA数据结构与算法题目集(中文) 7-36 社交网络图中结点的“重要性”计算 (30 分)
PTA数据结构与算法题目集(中文) 7-36 社交网络图中结点的“重要性”计算 (30 分) 7-36 社交网络图中结点的“重要性”计算 (30 分) 在社交网络中,个人或单位(结点)之间通过某 ...
- PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)
PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...
随机推荐
- U-boot分析与移植(3)----U-boot stage2分析
一来到void start_armboot (void)函数,马上出现两个很重要的数据结构gd_t和bd_t 1.gd_t : global data数据结构定义,位于文件 include/asm-a ...
- C#通用模块专题
开源 程序设计 常用组件 加载图片,播放音乐.视频,摄像头拍照 文件读写(txt.xml.自定义文件格式(后缀名)) 串口通信 稳定的串口读写:http://blog.csdn.net/kolvin2 ...
- Flask之邮件扩展
4.4 Flask—Mail 在开发过程中,很多应用程序都需要通过邮件提醒用户,Flask的扩展包Flask-Mail通过包装了Python内置的smtplib包,可以用在Flask程序中发送邮件. ...
- Vulkan Tutorial 09 图像与视图
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 使用任何的VkImage,包括在交换链或者渲染管线中的,我们都需要创建VkImage ...
- file_get_content() 超时
set_time_limit 只能影响php 程序的超时时间. file_get_contents 读取的是URL的超时时间. 因此 set_limit_limit 对file_get_conte ...
- Lambda03 方法引用、类型判断、变量引用
1 方法引用 1.1 方法引用的好处 方法引用结合 Lambda 可以引用已存在的方法,省略很多编码,而且可读性更强,它可以自动装配参数与返回值. 在编写lambda表达式的时候可以通过方法引用的方式 ...
- 2-JRE System Libraty [eclipse-mars](unbound)
- PC端QT源码编译
转载:http://blog.sina.com.cn/s/blog_c2b97b1d01016x1i.html 1.下载源码(前面已经提到了) 选择合适自己的源码. 先用"uname -a& ...
- Requests接口测试(四)
Python序列化和反序列化 啥是序列化?啥是反序列化?这两个词听起来优点高大上的意思,其实呢不然,很简单的可以理解为: 序列化:将python的数据对象编码转换为json格式的字符串 反序列化:将j ...
- css总结16:HTML5 多媒体音频(Audio)视频(video )
1 显示嵌入网页中的 MP3 文件: <embed height="50" width="100" src="horse.mp3"&g ...