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. Linux下javaweb环境搭建

    步骤: 1.使用远程工具连接上服务器,例如xsheel(ssh).filezilla(ftp) 2.JDK安装及相关配置 3.Mysql安装及相关配置 4.Tomcat安装及相关配置 5.项目部署及启 ...

  2. UVaLive 4043 Ants (最佳完美匹配)

    题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交. 析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可 ...

  3. mysql event 入门

    delimiter | CREATE EVENT statistics_event ON SCHEDULE EVERY DAY STARTS CONCAT(CURRENT_DATE(), ' 00:0 ...

  4. Alpha冲刺 - (6/10)

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 基于ssm框架的前后端交互测试,结合 ...

  5. OpenGL中的需要注意的细节问题

    OpenGL中的需要注意的细节问题 1. 虽然我们使用Windows的BMP文件作为纹理时,一般是蓝色的像素在最前,其真实的格式为GL_BGR而不是GL_RGB,在数据的顺序上有所 不同,但因为同样是 ...

  6. Oracle EBS Standard Package Function Add User & Resp

    Oracle EBS Standard Package Function Add User & Resp. fnd_user_pkg.CreateUser; fnd_user_pkg.AddR ...

  7. [C#]读取指定路径的配置文件[转]

    ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @"C:\App.c ...

  8. hibernate 中 fetch=FetchType.LAZY 懒加载失败处理

    对这种懒加载问题,最后的做法是利用Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,一直保持这个Session,使得Hi ...

  9. FTPClient用法

      某些数据交换,我们需要通过ftp来完成. sun.net.ftp.FtpClient 可以帮助我们进行一些简单的ftp客户端功能:下载.上传文件. 但如遇到创建目录之类的就无能为力了,我们只好利用 ...

  10. CI-Excel-Generation-Library php导出excel乱码。

    修改 private function generate($headers, $data) {        $this->set_headers();        $data = " ...