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 分)的更多相关文章

  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. 浙大数据结构课后习题 练习三 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 ...

  6. 7-3 树的同构(25 分) JAVA

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

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

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

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

  9. PTA 树的同构 (25分)

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

随机推荐

  1. Gulp 给所有静态文件引用加版本号

    在juqery和easyui 盛行的年代许多项目采用纯静态页面去构建前端框架从而实现前后端分离的目的.项目开发周期内往往会频繁修改更新某个文件,当你将文件更新到服务器后客户端由于缓存问题而出现显示异常 ...

  2. Kubernetes Storage Persistent Volumes

    链接:https://kubernetes.io/docs/concepts/storage/persistent-volumes/ 支持的参数,比如mountOptions在这里可以找到 删除正在被 ...

  3. js数据类型及变量知识(一)

    1.js中基本数据类型有哪些?    基本数据类型:        undefined.number.string.boolean.null.[object] object[引用数据类型]       ...

  4. Linux目录结构说明

    文件系统层级标准(filesystem hierarchy standard,FHS). http://www.pathname.com/fhs/pub/fhs-2.3.html 以下是对这些目录的解 ...

  5. 推荐一个去除图片人物背景的工具Removebg

    可以在线使用,url:https://www.remove.bg/users/sign_in 用邮箱免注册一个免费账号: 注册的邮箱会收到一封激活账号的邮件: 点击Activate account后激 ...

  6. http请求的几种content-type

    1. 2.二进制文件 3.form-data 数据:a=1 总结: 1.对于我自己的写的服务来说,只要是raw,接收到的都是二进制字符: 2.如果是urlencode,接收到的是拼接的字符串(和使用p ...

  7. MySQL用户与权限

    用户连接到mysql,并做各种查询,在用户和服务器中间分为两个阶段: 1:用户是否有权连接上来 2:用户是否有权执行此操作(如select,update等等) 先看第一个阶段:服务器如何判断用户是否有 ...

  8. MySQL创建用户和加限权

    目录 1.权限管理 1.1对新用户增删改 1.2对当前的用户授权管理 1.权限管理 ​ 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete ...

  9. Centos7源码部署apache/httpd服务

    httpd:是一个提供网站服务的程序 监听端口:80 环境准备: Linux CentOS7.3系统 使用一台服务端,一台客户端即可: 一.安装httpd 1:安装 [root@localhost ~ ...

  10. Pthon魔术方法(Magic Methods)-可视化

    Pthon魔术方法(Magic Methods)-可视化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于可视化的魔术方法简介 __str__: str()函数,format ...