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行对应编号第 ...
随机推荐
- 再议GCC编译时的静态库依赖次顺问题
假设有如三个源代码文件: $ cat a.cpp void a() { } $ cat b.cpp extern void a(); void b() { a(); // 调用a.cpp中的a() } ...
- [转]Newtonsoft.Json 序列化和反序列化 时间格式
本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...
- handsontable-developer guide-load and save
不过handsontable不能用jquery取对象 var $$ = function(id) { return document.getElementById(id); }, container ...
- 在改变某一项目的svn的地址,relocate
不要忘记删除原有.svn文件.
- 学习python的第五天
4.30自我总结 一复习 1.查看数据类型 #数值10的位置 print(di(10)) #数值10的样式 print(type(10)) 2.关于变量的一些补充 a=1 b=1 c=1 #a,b,c ...
- Kubernetes 自动伸缩 auto-scaling
使用 Kubernetes 的客户能够迅速响应终端用户的请求,交付软件也比以往更快.但是,当你的服务增长速度比预期更快时,计算资源不够时,该怎么处理呢? 此时可以很自豪地说: Kubernetes 1 ...
- .NET框架源码解读之SSCLI的调试支持
阅读源码一个比较快的手段就是在调试器里阅读,这样可以在实际运行SSCLI的过程中,通过堆栈跟踪的方式查看完整的程序执行路径. 当在SSCLI环境里执行一个托管程序的时候,堆栈上通常有托管和非托管代码同 ...
- Unity&C# SingerMonoManager泛型单例
管理各种管理器 ///为什么需要单例 ///单例模式核心在于对于某个单例类,在系统中同时只存在唯一一个实例,并且该实例容易被外界所访问: ///避免创建过多的对象,意味着在内存中,只存在一个实例,减少 ...
- 后台生产验证码code和byte[]图片
引用命名空间 using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System ...
- ASP.NET Core使用Ping判断网络是否接通
static void Main(string[] args) { // 主机地址 string targetHost = "bing.com"; string data = &q ...