03-树2. List Leaves (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
struct node{
int l,r,f;
node(){
l=r=f=-;
}
};
node tree[];
/*void prefind(int h){//检验程序
cout<<h<<endl;
if(tree[h].l>=0)
prefind(tree[h].l);
if(tree[h].r>=0)
prefind(tree[h].r);
}*/
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
//freopen("D:\\OUTPUT.txt","w",stdout);
int n;
string s;
scanf("%d",&n); //cout<<n<<endl; int i,num,j; /*for(i=0;i<n;i++){
cout<<"f: "<<tree[i].f<<endl;
cout<<"l: "<<tree[i].l<<endl;
cout<<"r: "<<tree[i].r<<endl;
}*/ for(i=;i<n;i++){
cin>>s;
if(s!="-"){
num=;
j=;
while(j<s.length()){
num*=;
num+=s[j++]-'';
}
tree[i].l=num;
tree[num].f=i;
}
cin>>s;
if(s!="-"){
num=;
j=;
while(j<s.length()){
num*=;
num+=s[j++]-'';
}
tree[i].r=num;
tree[num].f=i;
}
}
int h;
for(i=;i<n;i++){
if(tree[i].f<){
h=i;
break;
}
} //cout<<h<<endl;
//prefind(h); int p;
queue<int> q,qq;
q.push(h);
while(!q.empty()){
p=q.front();
q.pop(); //cout<<p<<endl; if(tree[p].l>=||tree[p].r>=){
if(tree[p].l>=)
q.push(tree[p].l);
if(tree[p].r>=)
q.push(tree[p].r);
}
else{
qq.push(p);
}
} //cout<<h<<endl; if(!qq.empty()){
printf("%d",qq.front());
qq.pop();
}
while(!qq.empty()){
printf(" %d",qq.front());
qq.pop();
}
printf("\n");
return ;
}

pat03-树2. List Leaves (25)的更多相关文章

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

  2. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  3. 03-树1 树的同构(25 point(s)) 【Tree】

    03-树1 树的同构(25 point(s)) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为 ...

  4. PTA 03-树2 List Leaves (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves   (25分) Given a tree, you ...

  5. 03-树1. List Leaves (25)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  6. 7-4 List Leaves (25分) JAVA

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  7. L2-006 树的遍历 (25 分)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...

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

  9. 浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

随机推荐

  1. CHOCBase

    1.基础常用知识点 1.1 监听某个对象变化的四种方式 代理监听Delegate Notification通知 KVO键值监听 Block代码块 addTarget方法 1.2 音频视频相册等 2.常 ...

  2. 上课总结-数据库Chapter2: 关系数据库

    Chapter2: 关系数据库 一.搞懂主键 外键关系 主键(主码):能唯一标识一个元组的某一属性组. 外键:不是这组数据的主键 但是另一组数据的唯一主键(当这组数据的主键有2个时 可以作为外键) 例 ...

  3. git配置本地环境(phpstudy/tortoisegit/git等)

    1.下载安装phpstudy 2.下载安装git 下载地址:https://git-scm.com/downloads 3.下载安装tortoisegit,电脑64位就下载这个,如图: 4.下载安装“ ...

  4. leecode刷题(5)-- 只出现一次的数字

    leecode刷题(5)-- 只出现一次的数字 只出现一次的数字 描述: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具 ...

  5. np.random.seed(0)的作用:作用:使得随机数据可预测。

    >>>> numpy.random.seed(0) ; numpy.random.rand(4) array([ 0.55,  0.72,  0.6 ,  0.54]) > ...

  6. vue中axios的使用与封装

    分享下我自己的axios封装axios是个很好用的插件,都是一些params对象,所以很方便做一些统一处理 当然首先是npm安装axios 很简单$ npm install axios --save在 ...

  7. 最短路径SPFA算法(邻接表存法)

    queue <int> Q; void SPFA (int s) { int i, v; for(int i=0; i<=n; i++) dist[i]=INF; //初始化每点i到 ...

  8. rabbitMq使用学习笔记

    rabbitmq的工作原理: MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced MessageQueue 高级消息队列协议) ...

  9. SQL语句之表操作

    SQL语句系列 1.SQL语句之行操作 2.SQL语句之表操作 3.SQL语句之数据库操作 4.SQL语句之用户管理 写在前面 在上一篇博文里面我整理了“行”级别的操作,分别是“增(insert).删 ...

  10. Flask 知识点

    flask run时候端口占用的问题 终端 lsof -i:5000 kill <端口号> 强制删除 kill -s 9 <端口号> 给网页标题添加icon {% block ...