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行对应编号第 ...
随机推荐
- 四种方法获取可执行程序的文件路径(.NET Core / .NET Framework)
原文:四种方法获取可执行程序的文件路径(.NET Core / .NET Framework) 本文介绍四种不同的获取可执行程序文件路径的方法.适用于 .NET Core 以及 .NET Framew ...
- C# vb .net实现棕褐色效果特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的棕褐色效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一 ...
- webpack 打包器
创建目录mkdir demo && cd demo 产生package.json执行 npm init -y 先全局安装webpack和webpack-clinpm install w ...
- 自学Python编程的第六天(最后代码有更好的请告诉我)----------来自苦逼的转行人
2019-09-16-23:09:06 自学Python的第六天,也是写博客的第六天 今天学的内容是有关dict字典的用法 看视频加上练习,目前还没遇到有难点,但是感觉很不好的样子 没有难点以后突然出 ...
- Java8新特性:接口
Java接口本身没有任何实现,因为Java接口不涉及表象,而只描述public行为,所以Java接口比Java抽象类更抽象化. 以上是百度百科中对接口的定义,这个定义已经不准确. Java8对接口做了 ...
- JavaScript insertAdjacentHTML()的使用
含义: insertAdjacentHTML() 将指定的文本解析为HTML或XML,并将结果节点插入到DOM树中的指定位置.它不会重新解析它正在使用的元素,因此它不会破坏元素内的现有元素.这避免了额 ...
- bootstrap fileinput实现限制图片上传数量及如何控制分批多次上传
废话没有,直奔主题 问题点: fileinput提供了一个maxFileCount用于限制图片上传的数量,设置maxFileCount为1时,一次性选择超过一张会有如下提示: 当选择一张,不点上传,再 ...
- Android存储及getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()区别
存储介绍 Android系统分为内部存储和外部存储,内部存储是手机系统自带的存储,一般空间都比较小,外部存储一般是SD卡的存储,空间一般都比较大,但不一定可用或者剩余空间可能不足.一般我们存储内容都会 ...
- 【DATAGUARD】物理dg配置客户端无缝切换 (八.1)--Data Guard Broker 的配置
[DATAGUARD]物理dg配置客户端无缝切换 (八.1)--Data Guard Broker 的配置 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读 各位技 ...
- day 09作业 预科
作业 1.简述定义函数的三种方式. 定义函数的三种方式为空函数,有参函数和无参函数 2.简述函数的返回值. 如果函数没有返回值,会直接返回到None: 函数可以通过return返回出返回值: retu ...