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 h[maxn],leaf[maxn],num = ;
bool isRoot[maxn]; int charToint(char c){
if(c == '-') return -;
else{
isRoot[c-''] = false;
//printf("isRoot[%d] == %d\n",c-'0',isRoot[c-'0']);
return c-'';
}
} void print(int i){
if(num == ){
printf("%d",i);
num++;
}
else printf(" %d",i);
} void BFS(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
//printf("1\n");
int now = q.front();
q.pop();
if(node[now].lchild == - && node[now].rchild == -) print(now);
if(node[now].lchild != -) q.push(node[now].lchild);
if(node[now].rchild != -) q.push(node[now].rchild);
}
} int main(){
int n;
scanf("%d\n",&n);
for(int i = ; i < n; i++){
isRoot[i] = true;
}
char a,b;
for(int i = ; i < n; i++){
scanf("%c %c",&a,&b);
getchar();
int u = charToint(a);
int v = charToint(b);
node[i].lchild = u;
node[i].rchild = v;
}
int root;
for(int i = ; i < n; i++){
if(isRoot[i] == true) root = i;
}
//printf("root == %d\n",root); BFS(root);
return ;
}

03-树2 List Leaves (25 分)的更多相关文章

  1. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  2. 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 ...

  3. 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 ...

  4. L2-006 树的遍历 (25 分)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...

  5. 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 ...

  6. 浙大数据结构课后习题 练习三 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. 7-3 树的同构(25 分) JAVA

    给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...

  8. PTA 03-树1 树的同构 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

  9. 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 ...

  10. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

随机推荐

  1. selenium+phantomjs解析JS

    背景知识: PhantomJS 是一个基于WebKit的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, J ...

  2. [GO]数组的比较和赋值

    package main import "fmt" func main() { //支持比较,只支持==或!=,比较是不是每一个元素都一样,2个数据比较,数据类型要一样 a := ...

  3. (转)Asp.Net底层原理(三、Asp.Net请求响应过程)

    原文地址:http://www.cnblogs.com/liuhf939/archive/2013/09/16/3324753.html 在之前,我们写了自己的Asp.Net框架,对整个流程有了一个大 ...

  4. CMake使用技巧

    前面有提到使用CMake.很多朋友提到也用过一下,没感觉它有什么好用,不知道怎么用之类. 我必要来说明一下. CMake的语法比较差,不是很优美,不是它不能用一个更好的语法,而是有一个关键优势:简单. ...

  5. 关于 ASP.NET 的 CompilationMode="Never" 性能问题

    今天在优化系统性的时候,想到了 ASP.NET 里的 CompilationMode="Never", 因为用户控件里没有任何代码, 只有控件,把用户控件的编译模式改为 Never ...

  6. C语言/C++编程学习三种循环用法和区别

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  7. Javascript中对数组处理的函数汇总

    数组类型是JS中非常常见的类型,而且JS里的数组与其他多数语言中的数组有所不同,它的数组里每一项可以存放任何一种类型的数据,也就是说数组的第一项放的是字符串,第二项可以放数字或对象都没问题.而且JS中 ...

  8. 「POJ 2182」 Lost Cows

    题目链接 戳这 题目大意 \(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些 ...

  9. 正确处理类的复合关系------新标准c++程序设计

    假设要编写一个小区养狗管理程序,该程序需要一个“主人”类,还需要一个“狗”类.狗是有主人的,主人也有狗.假定狗只有一个主人,但一个主人可以有最多10条狗.该如何处理“主人”类和“狗”类的关系呢?下面是 ...

  10. HSSFWorkbook导出表格使用

    HSSFWorkbook                      excel的文档对象 HSSFSheet                         excel的表单 HSSFRow      ...