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 ...
随机推荐
- 求整数数组(长度为n),出现大于2/n次数的数字
条件:时间复杂度是O(n),空间复杂度是O(1) 方法1:标记法 , , , , , , , , , , , , , }; int len = arr.Length; int[] c = new in ...
- Android日期时间选择器DatePicker、TimePicker日期时间改变事件响应(Android学习笔记)
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Daily translation 3th
Source url:http://www.nzherald.co.nz/education/news/article.cfm?c_id=35&objectid=11149719 //plac ...
- 以太坊系列之六: p2p模块--以太坊源码学习
p2p模块 p2p模块对外暴露了Server关键结构,帮助上层管理复杂的p2p网路,使其集中于Protocol的实现,只关注于数据的传输. Server使用discover模块,在指定的UDP端口管理 ...
- 「POJ 2182」 Lost Cows
题目链接 戳这 题目大意 \(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些 ...
- Swoole http server + yaf, swoole socket server + protobuf 等小结
拥抱swoole, 拥抱更好的php Swoole 是什么? Yaf 是什么? 接触swoole已经4年多了,一直没有好好静下心来学习.一直在做web端的应用,对网络协议和常驻内存型服务器一窍不通.一 ...
- cookie,session 的概念以及在django中的用法,以及cbv装饰器用法
cookie的由来: 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不会直接影响后 ...
- 【AGC013D】Pilling Up dp
Description 红蓝球各无限多个. 初始时随意地从中选择 n 个, 扔入箱子 初始有一个空的序列 接下来依次做 m 组操作, 每组操作为依次执行下述三个步骤 (1) 从箱子中取出一个求插入序列 ...
- 杀死进程命令 kill
一般kill命令和ps命令结合使用, 例:现在想杀死telnet的进程 1.在所有进程中查看telnet命令 ps -ef |grep telnet 2.根据上面命令查到的进程id,如pid 是 xx ...
- 1、TensorFlow简介
参考:http://www.tensorfly.cn/tfdoc/get_started/basic_usage.html 1.用TensorFlow构造一个简单的线性拟合: # -*- coding ...