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. Hyperledger Fabric Ordering Service过程

    排序服务在超级账本 Fabric 网络中起到十分核心的作用.所有交易在发送给 Committer 进行验证接受之前,需要先经过排序服务进行全局排序. 在目前架构中,排序服务的功能被抽取出来,作为单独的 ...

  2. sklearn scoring . xgboost.train . ---> rsme

    http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 3.3.1. The scoring pa ...

  3. 洛谷P2569 [SCOI2010]股票交易

    P2569 [SCOI2010]股票交易 题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股 ...

  4. (BST)升序数组变为BST树

    题目:给定一个数组,其中元素按升序排序,将其转换为高度平衡BST. 思路:因为是升序数组,那么中间的数字一定是根节点值,然后在对左右两边的数组进行查找根节点的递归.一次处理左右子树. /** * De ...

  5. 单个控件textbox只支持在英文状态下输入所需的字符串

    也就是它的属性: Imemode的属性 设置成off就可以了 ,就不会受到所输入的中文汉字了.

  6. LightOJ 1258 Making Huge Palindromes (Manacher)

    题意:给定上一个串,让你在后面添加一些字符,使得这个串成为一个回文串. 析:先用manacher算法进行处理如果发现有字符匹配超过最长的了,结束匹配,答案就是该字符前面那个长度加上该串原来的长度. 代 ...

  7. BZOJ 3083 遥远的国度(树链剖分+LCA)

    Description 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要z ...

  8. Ubuntu不能上网解决办法

     一.设置IP.网关.DNS 新安装的Ubuntu系统ifconfig后发现没有ip,所以要设置IP.网关.DNS等,编辑 /etc/networking/interfases sudo vi /et ...

  9. Windows + python + pywinauto 搭建自动化测试环境

    最近公司在搞测试, 单纯的人工去测试需要花费太多的人力物力以及时间, 所以准备用Python做一套自动化测试来使用. 本文中使用的是Python3.6.8  和 pywin32-224.win-amd ...

  10. rsync服务搭建--2018.5.8 [优化后最终版]

    2018年5月8日 22:09:38 第一步配置基础环境(按照自己的规划配置并非每人的环境都一致) 第一台服务器(RSYNC服务器): rsync外网地址:10.0.0.41  rsync内网地址:1 ...