pat03-树2. List Leaves (25)
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.
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)的更多相关文章
- 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 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- 03-树1 树的同构(25 point(s)) 【Tree】
03-树1 树的同构(25 point(s)) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为 ...
- 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 ...
- 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 ...
- 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 ...
- L2-006 树的遍历 (25 分)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...
- 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 ...
- 浙大数据结构课后习题 练习三 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 ...
随机推荐
- 停止Nginx服务
查询nginx进程信息 chen@ubuntu:~$ ps -ef |grep nginx root : ? :: nginx: master process /usr/sbin/nginx -g d ...
- vs2015编译ffmpeg 出现错误rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 ___iob_func
vs2015编译ffmpeg(版本3.0.2)引用外部库文件librtmp出现以下错误: rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 __imp__st ...
- Linux 运维工程师经典面试题合集(不断更新中 ...)
写在前面的话 做运维以来,关注了很多关于互联网技术,培训等乱七八糟的公众号,时不时的就会推一些各种公司的面试题过来. 大致看了一下,发现自己很多知识要么遗忘了,要么很难说出个所以然,所以这里对那些个人 ...
- 1442: Neo 的简单字符串(字符串)
1442: Neo 的简单字符串 时间限制: 10 Sec 内存限制: 128 MB 提交: 9 解决: 3 统计 题目描述 Neo 给你一系列字符串,请你输出字符串中的不同单词个数以及总单词个数. ...
- 【BlockingQueue】BlockingQueue 阻塞队列实现
前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便 ...
- Spring boot redis自增编号控制 踩坑
近段期间,公司 接手一个订单号生成服务,规则的话已经由项目经理他们规定好了,主要是后面的四位数代表的关于当前订单号已经执行第几个了.而这里面有一个要求就是支持分布式.为了实现这个东西,刚开始我使用了r ...
- SDK 开发 .a .framework .bundle (xcode引用) 依赖sdk工程
一. 静态库.a 1.创建静态库工程 Cocoa Touch Static Libray ,然后可以创建一个测试视图 TestView 2.暴露头文件 -> Build Phases--> ...
- Linux磁盘占满 no space left on device
假如当前文件删除了,如果还有其他进程还在使用这个文件,这个文件删不干净:https://www.cnblogs.com/heyonggang/p/3644736.html 在Linux下查看磁盘空间使 ...
- CentOS 6.* 安装node
CentOS6 安装node 使用node -v 是报错 ./node: error while loading shared libraries: libstdc++.so.6: cannot op ...
- 洛谷 P3233 [HNOI2014]世界树(虚树+dp)
题面 luogu 题解 数据范围已经告诉我们是虚树了,考虑如何在虚树上面\(dp\) 以下摘自hzwer博客: 构建虚树以后两遍dp处理出虚树上每个点最近的议事处 然后枚举虚树上每一条边,考虑其对两端 ...