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. Mysql表字段命令alter add

    alter add命令用来增加表的字段. alter add命令格式:alter table 表名 add字段 类型 其他; 例如,在表MyClass中添加了一个字段passtest,类型为int(4 ...

  2. .Net MVC如何渲染带有网页标签的字符串

    有时候我们在解析一段文字时,可能文字中会包含网页上的标签,如div.p等等.那么如果将这种文字渲染成对应的标签效果呢?如图,最近博主就拿到了这么一段字符串(如图) 由于中间带有很多特殊字符,用Html ...

  3. [個人紀錄] WindowsLiveWriter 插入代碼跳出錯誤

    跳出找不到設定檔Can’t load configruaration fromC:\Users\…\AppData\Roaming\Windows Live Writer\WindowsLiveWri ...

  4. 静态工具类注入service的方法

    http://blog.sina.com.cn/s/blog_6e2d53050102wl3x.html

  5. ASP.NET SignalR 系列(六)之连接事件

    本章主要介绍下SignalR自带的连接事件 其实再前面的示例中,有出现了一些事件的重载,比如 public override Task OnConnected() 下面简单介绍一下SignalR提供了 ...

  6. linux搭建GitLab

    GitLab CentOS6 1. 安装VMware和CentOS 2. 安装必备Linux插件 3. 准备安装GitLab 4. 开始安装GitLab 5. 配置GitLab 6. 启动GitLab ...

  7. P2711 小行星 (最大流)

    题目 P2711 小行星 解析 这道题挺巧妙的,乍一看是空间上的,无从下手,稍微转换一下就可以了. 看到题目,求消除这些行星的最少次数,就是求最小割,也就是求最大流,考虑怎样建图. 考虑当我们消去一个 ...

  8. jq+swiper 实现今日头条App的选项卡效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 教你如何配置linux用户实现禁止ssh登陆机器但可用sftp登录!

    构想和目标最近有个这样的诉求:基于对线上服务器的保密和安全,不希望开发人员直接登录线上服务器,因为登录服务器的权限太多难以管控,如直接修改代码.系统配置,并且也直接连上mysql.因此希望能限制开发人 ...

  10. 03-JavaScript语法介绍

    本篇主要关于原生JavaScript的介绍,其中包括其嵌入HTML页面方式,JavaScript的语法结构,以及贪吃蛇案例: 一.绪论 JavaScript是运行在浏览器端的脚步语言,JavaScri ...