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);
}
}
*/

最新文章

  1. 1.bootstrap练习笔记-导航条
  2. (译) Conditional Variational Autoencoders 条件式变换自编码机
  3. 线程小demo
  4. 如何限制虚拟主机可使用的CPU资源
  5. MAC OS JAVA环境变量配置
  6. 转载jquery $(document).ready() 与window.onload的区别
  7. UVA 10716 Evil Straw Warts Live(贪心)
  8. ASP.NET实现二级域名(多用户,多商店)
  9. Android开发系列(十八):自己定义控件样式在drawable目录下的XML实现
  10. MVC中一般为什么用IQueryable而不是用IList?用IQueryable比IList好在哪?
  11. Address already in use: JVM_Bind错误的解决
  12. ECOS-认证地址
  13. 牛客多校第三场 A- PACM Team 背包/记忆路径
  14. 文本分类学习 (十)构造机器学习Libsvm 的C# wrapper(调用c/c++动态链接库)
  15. lex yacc flex bison
  16. laravel iis搭建
  17. 【转】【OPenGL】OPenGL 画图板-- 中点算法画圆
  18. 表达式SpEL方式的属性注入
  19. JavaScript停止事件冒泡和取消事件默认行为
  20. scrapy抓取拉勾网职位信息(二)——拉勾网页面分析

热门文章

  1. C# vb .net实现倾斜效果滤镜
  2. Python进阶(十三)----面向对象
  3. 图解HTTP(三)
  4. Nginx配置SSL实现HTTPS访问
  5. Html-元素类型笔记
  6. python3中try异常调试 raise 异常抛出
  7. 京信通信成功打造自动化工厂(MES应用案例)
  8. trackingjs+websocket+百度人脸识别API,实现人脸签到
  9. Flask入门很轻松(三)—— 模板
  10. Docker02-重要概念