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 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...
随机推荐
- java多态介绍温故而知新
多态是同一个行为具有多个不同表现形式或形态的能力. 多态就是同一个接口,使用不同的实例而执行不同操作. 多态性是对象多种表现形式的体现. 现实中,比如我们按下 F1 键这个动作: 如果当前在 Flas ...
- mysql枚举类型与集合类型
枚举类型与集合类型 字段的值只能在给定范围中选择,如单选框,多选框 enum 单选 只能在给定的范围内选一个值,如性别 sex 男male/女female set 多选 在给定的范围内可以选择一个或一 ...
- 什么是Ajax和JSON,它们的优缺点
什么是Ajax??? 术语Ajax用来描述一组技术,它使浏览器可以为用户提供更为自然的浏览体验. Ajax它是“Asynchronous JavaScript + XML的简写” 定义Ajax: Aj ...
- python利用scapy模块写一个TCP路由追踪和扫描存活IP的脚本
前言: 没有前言 0x01 from scapy.all import * import sys from socket import * import os from threading impor ...
- http协议请求响应内容示例
POST http://www.cytxl.com.cn/api/common/login.php?XDEBUG_SESSION_START=netbeans-xdebug HTTP/1.1 Host ...
- CSS JQuyer 元素选择
$(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class="intro" ...
- JS中使用正则表达式
- Django模板语言之组合搜索
url.py from django.conf.urls import url from django.contrib import admin from app01 import views url ...
- 用插件NPOI读写excel
1.用插件NPOIusing NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using NPOI.HSSF.UserModel; public class E ...
- 张超超OC基础回顾04_实例变量修饰(@public),点语法,self关键字,多态,继承
零.实例变量修饰符 /* @public 就是实例变量修饰符 @public >可以在其它类中访问被public修饰的成员变量 >也可以在本类中访问被public修饰的成员变量 >可 ...