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 (≤) 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<queue>
using namespace std;
const int maxn = ;
struct Node{
int lchild,rchild;
}node[maxn]; int num = ;
bool isRoot[maxn]; int change(char c){
if(c == '-') return -;
else{
isRoot[c-''] = false;
return c-'';
}
} void print(int v){
if(num == ){
printf("%d",v);
num++;
}else{
printf(" %d",v);
}
} void BFS(int root){
int front = -,rear = -;
int q[maxn];
q[++front] = root;
while(front!= rear){
int now = q[++rear];
if(node[now].lchild == - && node[now].rchild == -) print(now);
if(node[now].lchild != -) q[++front] = node[now].lchild;
if(node[now].rchild != -) q[++front] = node[now].rchild;
}
// queue<int> q;
// q.push(root);
// while(!q.empty()){
// int now = q.front();
// q.pop();
// if(node[now].lchild==-1&&node[now].rchild==-1)
// print(now);
// if(node[now].lchild != -1)
// q.push(node[now].lchild);
// if(node[now].rchild != -1)
// q.push(node[now].rchild);
// }
} int main(){
int n;
scanf("%d",&n);
for(int i = ; i < n; i++){
isRoot[i] = true;
}
for(int i = ; i < n; i++){
char a,b;
getchar();
scanf("%c %c",&a,&b);
node[i].lchild = change(a);
node[i].rchild = change(b);
}
int root;
for(int i = ; i < n; i++){
if(isRoot[i] == true){
root = i;
break;
}
}
BFS(root);
return ;
}

03-树2 List Leaves (25 分)的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

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

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

  5. 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 ...

  6. 浙大数据结构课后习题 练习三 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 ...

  7. 7-3 树的同构(25 分) JAVA

    给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...

  8. PTA 03-树1 树的同构 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

  9. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  10. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

随机推荐

  1. android listView布局等分列

    android listView布局4等分列. 必须要加上<RelativeLayout 在外层,不然等分不起作用 <RelativeLayout xmlns:android=" ...

  2. 一文读懂spark yarn集群搭建

    文是超简单的spark yarn配置教程: yarn是hadoop的一个子项目,目的是用于管理分布式计算资源,在yarn上面搭建spark集群需要配置好hadoop和spark.我在搭建集群的时候有3 ...

  3. Google Tango Java SDK开发:Motion Tracking 运动追踪

    Java API Motion Tracking Tutorial运动追踪教程 This page describes how the Java API handles motion tracking ...

  4. ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏

    Ranking System Time Limit: 2 Seconds      Memory Limit: 65536 KB Few weeks ago, a famous software co ...

  5. Poj2231 Moo Volume 2017-03-11 22:58 30人阅读 评论(0) 收藏

    Moo Volume Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22104   Accepted: 6692 Descr ...

  6. MFC中的DLL、LIb文件的创建、使用

     动态链接库Dynamic-Linked Lib 的创建与使用 动态链接库(Dynamic Link Library 或者 Dynamic-link Library,缩写为 DLL),是微软公司在微软 ...

  7. Web 协议 HTTP1.0 HTTP1.1 SPDY HTTP2.0

    Web 协议 HTTP1.0 HTTP1.1 SPDY HTTP2.0 HTTP1.0 VS HTTP1.1 长连接HTTP 1.0需要使用keep-alive参数来告知服务器端要建立一个长连接,而H ...

  8. [zepto]源码学习

    $() : zepto选择器,与jquery选择器类似,但是不支持jquery的拓展css支持(:first,:eq,:last...) $("div")选择页面内的全部div元素 ...

  9. PostgreSQL递归查询

    原料 --创建组织架构表 create table "Org"( "OrgId" ) primary key, "ParentId" ), ...

  10. ASP.NET Core根据环境切换NLog配置

    1.新建NLog配置文件,名称分别为nlog.config和nlog.debug.config <?xml version="1.0"?> <nlog xmlns ...