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 ...
随机推荐
- TSQL--删除正在运行的数据库
); SET @dbName='DB1_SNAP' BEGIN TRY --===================================== --查找当前数据库所有连接并删除 DECLARE ...
- Socket网络通讯
网络编程 使用C#进行网络编程时,通常都需要用到System.Net命名空间.System.Net.Sockets命名空间和System.Net.Mail命名空间: 1. System.Net命名空间 ...
- Hexo基本使用
1. 简述 Hexo是一个基于Node.js的博客系统,常用于搭建在GitHub上. 其本质其实是一个基于Node.js的内嵌webserver的程序,可以通过一些命令根据markdown生成静态页面 ...
- 【连载】redis库存操作,分布式锁的四种实现方式[二]--基于Redisson实现分布式锁
一.redisson介绍 redisson实现了分布式和可扩展的java数据结构,支持的数据结构有:List, Set, Map, Queue, SortedSet, ConcureentMap, L ...
- 题解 P1720 【月落乌啼算钱】
题目链接 定义一个函数比较好求. #include<bits/stdc++.h>//万能头文件 using namespace std; double F(int x)//定义函数,为了保 ...
- uoj #111. 【APIO2015】Jakarta Skyscrapers
#111. [APIO2015]Jakarta Skyscrapers 印尼首都雅加达市有 NN 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 00 到 N−1N−1.除了这 NN 座摩 ...
- 洛谷P2765 魔术球问题(最大流)
传送门 %%%KSkun大佬 话说明明是网络流……这题竟然还有打表找规律和纯贪心AC的……都是神犇啊…… 来说一下如何建图.首先把每一个点拆成$X_i$和$Y_i$,然后$S$向$X_i$连一条容量为 ...
- vs2017启动iis局域网无法访问解决
1.找到IISExpress的配置文件,位于 <文档>/IISExpress/config文件夹下,打开applicationhost.config,找到如下代码: <site na ...
- Python——requests的安装及入门-贴吧爬虫
一.windows平台下requests的安装 1.win+R,输入cmd,打开命令行窗口,输入命令:pip install requests ,即可自动安装库成功 2.输入命令:pip list,即 ...
- 简单的IDEA破解到2099年
原文链接:https://www.cnblogs.com/duende99/p/10038640.html 破解方式有2种,第一种比较方便 第一种比较方便1.使用注册码破解:http://idea.l ...