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的更多相关文章

  1. PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-43字符串关键字的散列映射 (25 分) 7-43 字符串关键字的散列映射 (25 分)   给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义 ...

  2. PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-42整型关键字的散列映射 (25 分) 7-42 整型关键字的散列映射 (25 分)   给定一系列整型关键字和素数P,用除留余数法定义的散列函数将关键字映射 ...

  3. PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)

    PTA数据结构与算法题目集(中文)  7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分)   计算机程序设计能力考试(Programming Ability Test,简称P ...

  4. PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)

    PTA数据结构与算法题目集(中文)  7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分)   每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...

  5. PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)

    PTA数据结构与算法题目集(中文)  7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分)   在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...

  6. PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)

    PTA数据结构与算法题目集(中文)  7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分)   胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...

  7. PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)

    PTA数据结构与算法题目集(中文)  7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分)   Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...

  8. PTA数据结构与算法题目集(中文) 7-36 社交网络图中结点的“重要性”计算 (30 分)

    PTA数据结构与算法题目集(中文)  7-36 社交网络图中结点的“重要性”计算 (30 分) 7-36 社交网络图中结点的“重要性”计算 (30 分)   在社交网络中,个人或单位(结点)之间通过某 ...

  9. PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)

    PTA数据结构与算法题目集(中文)  7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...

随机推荐

  1. puclic 页面公共CSS样式

    body, div, dl, dt, dd, ul, ol, li, pre, form, fieldset, blockquote, h1, h2, h3, h4, h5, h6,p{ paddin ...

  2. leetcode709

    string toLowerCase(string str) { transform(str.begin(), str.end(), str.begin(), ::tolower); return s ...

  3. SQL中对数据截取替换

    REPLACE(要修改字段名,‘被替换的特定字符’,‘替换的字符’) 表结构和插入数据 CREATE DATABASE TEST GO USE TEST GO CREATE TABLE TestTab ...

  4. PHP内核介绍及扩展开发指南—Extensions 的编写(下)

    第一个参数是HashTable,在1.2.3节提到Zend使用HashTable来存储PHP函数,function_table用于指 定从哪个HashTable中获取函数.通常应该用CG(functi ...

  5. php扩展开发1--添加函数

    目标:便携php扩展 要求实现 输出hello word 首先用的是php7.0.3   centos7.1或者centos6.+ 1.1 RPM安装PHP rpm -Uvh https://mirr ...

  6. 443. String Compression字符串压缩

    [抄题]: Given an array of characters, compress it in-place. The length after compression must always b ...

  7. 性能优化之_android布局优化

    优化布局的的原则就是减少创建的对象的数量,setContentView话费onCreate到onResume中的大概99%的时间1.使用Relativelayout而不是LinearLayout,Li ...

  8. 为什么有时候在Windbg中下断点下不了呢??

    1.今天我发现我下断点的地方是我声明的变量,变量还仅仅是声明,没有赋值.因此不能下断点. 2.当不能下断点时,如何解决呢? (1)重启Vmware虚拟机.(2)在虚拟机中恢复“快照”.

  9. loj2512 [BJOI2018]链上二次求和

    传送门 分析 咕咕咕 代码 #include<iostream> #include<cstdio> #include<cstring> #include<st ...

  10. wordpress中add_action和add_filter

    add_action( string $tag, callable $function_to_add, int $priority = 10,int $accepted_args = 1 ) 官网是这 ...