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 left,right;
}node[maxn]; bool isRoot[maxn];
int num = ; void init();
int change(char c);
int FindRoot(int n);
void BFS(int root);
void print(int v); int main()
{
init();
int n;
char a,b;
scanf("%d",&n); for (int i= ; i < n; i++)
{
getchar();
scanf("%c %c",&a,&b);
node[i].left = change(a);
node[i].right = change(b);
} int root = FindRoot(n);
BFS(root);
return ;
} void init()
{
for (int i = ; i < maxn; i++)
{
isRoot[i] = true;
}
} int change(char c)
{
int iRet = -;
if (c != '-')
{
iRet = c - '';
isRoot[iRet] = false;
}
return iRet;
} int FindRoot(int n)
{
for (int i = ; i < n; i++)
{
if (isRoot[i])
{
return i;
}
}
} void BFS(int root)
{
#if 0 //使用数组模拟队列
int front = -; //头
int rear = -; //尾
int queue[maxn] = {}; queue[++front] = root;
while (front != rear)
{
int now = queue[++rear];
if (node[now].left == - && node[now].right == -)
{
print(now);
}
if (node[now].left != -)
{
queue[++front] = node[now].left;
}
if (node[now].right != -)
{
queue[++front] = node[now].right;
}
}
#else //使用c++的queue
queue<int> q;
q.push(root);
while (!q.empty())
{
int now = q.front();
q.pop();
if (node[now].left == - && node[now].right == -)
{
print(now);
}
if (node[now].left != -)
{
q.push(node[now].left);
}
if (node[now].right != -)
{
q.push(node[now].right);
}
} #endif
} void print(int v)
{
if ( == num)
{
printf("%d",v);
num++;
}
else
{
printf(" %d",v);
}
}
/*
void BFS(int root) 先序遍历
{
if (node[root].left == -1 && node[root].right == -1)
{
if (1 == num)
{
printf("%d",root);
num++;
}
else
{
printf(" %d",root);
}
return;
} if (node[root].left != -1)
{
BFS(node[root].left);
}
if (node[root].right != -1)
{
BFS(node[root].right);
}
}
*/
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 题目: 给定一棵二叉树的后序遍历和中序 ...
- 浙大数据结构课后习题 练习三 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行对应编号第 ...
随机推荐
- 拓展 - WebRTC 多视频网络拓扑之三种架构
众所周知,WebRTC非常适合点对点(即一对一)的音视频会话.然而,当我们的客户要求超越一对一,即一对多.多对一设置多对多的解决方案或者服务,那么问题就来了:“我们应该采用什么样的架构?” .简单的呢 ...
- js预编译环节 变量声明提升 函数声明整体提升
预编译四部曲 1.创建AO对象 2.找形参和变量声明,将变量和形参名作为AO属性名,值为undefined 3.将实参和形参统一 4.在函数体里面找函数声明,值赋予函数体 function fn(a) ...
- synchronized关键字的使用
synchronized关键字是java并发编程中常使用的同步锁,用于锁住方法或者代码块,锁代码块时可以是synchronized(this){}.synchronized(Object){}.syn ...
- 图记 2016.1.7 获取本地图片、Bitmap转image
这几天完成的内容有: 1.“添加图片”按钮 2.添加图片功能 遇到的问题: 我想要将添加图片按钮放在右下角,所以采用了相对布局,但是问题随之二来,因为将导航栏设置成了半透明,所以图片放到右下角之后,半 ...
- ElasticSearch(十二):Spring Data ElasticSearch 的使用(二)
在前一篇博文中,创建了Spring Data Elasticsearch工程,并且进行了简单的测试,此处对Spring Data Elasticsearch进行增删改查的操作. 1.增加 在之前工程的 ...
- linux设备驱动程序-i2c(1):i2c总线的添加与实现
linux设备驱动程序-i2c(1):i2c总线的添加与实现 (基于4.14内核版本) 在上一章节linux设备驱动程序-i2c(0)-i2c设备驱动源码实现中,我们演示了i2c设备驱动程序的源码实现 ...
- C++(四十六) — 异常处理机制、标准IO输入输出
1.异常处理机制 一般来说,异常处理就是在程序运行时对异常进行检测和控制.而在C++ 中,使用 try-throw-catch模式进行异常处理的机制. #include<iostream> ...
- Python爬取mn52网站美女图片以及图片防盗链的解决方法
防盗链原理 http标准协议中有专门的字段记录referer 一来可以追溯上一个入站地址是什么 二来对于资源文件,可以跟踪到包含显示他的网页地址是什么 因此所有防盗链方法都是基于这个Referer字段 ...
- An exception has occurred, use %tb to see the full traceback.----parser.parse_args()报错
一.报错: 原因: 由于在jupyter notebook中,args不为空. 二.问题解决 改成args = parser.parse_args(args=[])
- word2vec中的subsampling
http://d0evi1.com/word2vec-subsampling/ 为了度量这种罕见词与高频词间存在不平衡现象,我们使用一个简单的subsampling方法:训练集中的每个词wiwi,以下 ...