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 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...
随机推荐
- Android BindService中遇到的一个小问题
今天在使用BindService的时候遇到个小问题,我希望通过Bindservice获取到这个服务,然后执行服务内的某个自定义方法,如下: if(bindService==null){ Intent ...
- Deep Learning 学习笔记(6):神经网络( Neural Network )
神经元: 在神经网络的模型中,神经元可以表示如下 神经元的左边是其输入,包括变量x1.x2.x3与常数项1, 右边是神经元的输出 神经元的输出函数被称为激活函数(activation function ...
- Maven 国内源
maven的仓库好慢的说,还是配置一个国内的源吧.推荐aliyun 在maven/conf/settings.xml 文件里配置mirrors的子节点,添加如下mirror <mirror> ...
- 动态绑定事件-on
动态绑定事件 $(document).on("各种事件(如click.mousemove...)","事件对象(比如我点击class为.close的div,那么这里就是. ...
- UIThread
功能如下:点击Create则新创建一个窗口 一 . 资源中添加对话框,右键添加类MyDlg 双击初始对话框中的按钮,实现按钮功能:点击则创建一个对话框 CMyDialog* pDialog = new ...
- codeforce468DIV2——E. Game with String
题目 Vasya and Kolya play a game with a string, using the following rules. Initially, Kolya creates a ...
- linux下rsync命令详细整理
点评:rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一详细说明,需要了解的朋友可以参考下-在对rsync服务器配置结束以后,下一步就需要在客户端发出rsync命 ...
- [原创]SQL 把表中字段存储的逗号隔开内容转换成列表形式
我们日常开发中,不管是表设计问题抑或是其他什么原因,或多或少都会遇到一张表中有一个字段存储的内容是用逗号隔开的列表. 具体效果如下图: ------> 从左边图转换成右边图,像这种需求,我们难免 ...
- 3-java中String值为空字符串与null的判断方法
java中String值为空字符串与null的判断方法 2018年01月21日 14:53:45 阅读数:1189 Java空字符串与null的区别 1.类型 null表示的是一个对象的值,而不是一个 ...
- 如果你的资源贫乏,那么专注做好一件事将是你的唯一出路(no reading yet)
http://www.jianshu.com/p/8784f0fd7ab8/comments/1161511