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行对应编号第 ...
随机推荐
- Problem of Uninstall Cloudera: Cannot Add Hdfs and Reported Cannot Find CDH's bigtop-detect-javahome
1. Problem We wrote a shell script to uninstall Cloudera Manager(CM) that run in a cluster with 3 li ...
- 我们常说的CDN到底是什么?
程序员每天提及的无数词当中,有一个是「CDN」,Ta的中文名是「内容分发网络」,读中文是令人蒙逼的,英文名是Content Delivery Network. CDN有啥用呢?它主要用来解决什么问题呢 ...
- python 命令行工具 fire
简介 A library for automatically generating command line interfaces. Python Fire is a library for auto ...
- RocketMQ 运维指令
1.1. 控制台使用 RocketMQ 提供有控制台及一系列控制台命令,用于管理员对主题,集群,broker 等信息的管理 登录控制台 首先进入RocketMQ 工程,进入/RocketMQ/bin ...
- Windows与Linux相互远程桌面连接
对于远程桌面连接Linux,大家可能会第一时间想到使用VNC,,远程桌面Windows,大家第一时间会想到使用Windows自带的远程桌面.那么有没有办法,使得在Linux中可以远程Windo ...
- NideShop项目的安装部署教程
本文档为微信小程序商城NideShop项目的安装部署教程,欢迎star NideShop商城api服务:https://github.com/tumobi/nideshop NideShop微信小程序 ...
- IOC容器基本原理
1 IoC容器的概念 IoC容器就是具有依赖注入功能的容器,IoC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需直接在代码中new相关的对象,应用程序由IoC容器进行 ...
- 基于Struts2开发学生信息管理系统 源码
开发环境: Windows操作系统开发工具: Eclipse+Jdk+Tomcat+MYSQL数据库 运行效果图: 联系博主-Q:782827013
- bootstrap-treeview中文API 以及后台JSON数据处理
bootstrap-treeview 简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bo ...
- Winfrom PictureBox 设置图片自适应
初始状态 Bitmap bm = new Bitmap(Image.FromStream(System.Net.WebRequest.Create(new Uri(result.Result)).Ge ...