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 (≤) 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 分)的更多相关文章
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- 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 ...
- 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 ...
- 7-3 树的同构(25 分) JAVA
给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...
- PTA 03-树1 树的同构 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构 (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...
- 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 ...
- PTA 树的同构 (25分)
PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...
随机推荐
- 布局xml文件不能预览
原因:xml文件里,有一些值没有设置属性. 如: <LinearLayout android:layout_width="fill_ ...
- idea中代码补全
在IDEA中,默认的代码自动提示不够智能,现在配置成更加智能的方式. File-Settings-Editor-General-Code Completion中 把最上面的大小写敏感度改成none,下 ...
- Sypder 安装和使用
一.安装Spyder 我傻傻以为直接下载Spyder就可以用了,但我其实大错特错了.Spyder虽然提供科学计算,但是它还需要一个介于Python和其之间的框架,或者说,显示界面PyQt5.(PyQt ...
- php CI框架输出空行问题排查
今天在使用 curl 命令行工具调试一个功能时,发现输出的内容总是会在最开始莫名其妙的多一行空行: 项目框架是 php 的 CodeIgniter,感觉这种问题在网上不好查找,因为可以确定这个是业务出 ...
- 5、Docker架构和底层技术
5.1 Docker Platform Docker提供了一个开发,打包,运行APP的平台 把APP和底层infrastructure隔离开来 5.2 Docker Engine 后台进程(docke ...
- 更改mysql默认字符集 (转载)
1. service mysqld stop,停用mysql. 2. cp /etc/my.cnf /etc/my.cnf.bak,修改前做备份,这是个好习惯. 修改my.cnfvi /etc/my. ...
- [C#] ??雙問號的意思及用法
int? x = null; int y = x ?? -1; 上面二行中,第一行是將x變數放入null,為什麼int能放null,可以參考另一篇文章http://charleslin74.pixne ...
- App与微信WebAPP
我的App与微信搞上了 小麦积分墙摘自网络 最近有很多开发者关心的一个问题是如何提升app的下载量,透过现象开本质,app下载量的终极目标还是为多少客户提供了服务,抛开下载量KPI,app真心关心的问 ...
- WPF自定义ComboBox
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}"> &l ...
- ClamAV学习【7】——病毒库文件格式学习
搜查到一份详细的ClamAV病毒文件格式资料(http://download.csdn.net/detail/betabin/4215909),英文版,国内这资料不多的感觉. 重点看了下有关PE的病毒 ...