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 (≤10) 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <queue>
#include <algorithm> using namespace std; struct node
{
int ll;
int rr;
int data;
int dep;
int dfn;
}q[20]; struct N
{
int num;
int dep;
int dfn;
bool operator<(const N &dd)const{
if(dep==dd.dep)
return dd.dfn<dfn;
else
return dd.dep<dep;
}
}; int cnt;
void dfs_leaf(int root, int deep)
{
if(q[root].ll==-1 && q[root].rr==-1)
return;
if(q[root].ll!=-1){
int v=q[root].ll;
q[v].dep=deep+1;
q[v].dfn=cnt++;
dfs_leaf(v, deep+1);
}
if(q[root].rr!=-1){
int v=q[root].rr;
q[v].dep=deep+1;
q[v].dfn=cnt++;
dfs_leaf(v, deep+1);
}
} int main()
{
int n; scanf("%d%*c", &n);
int i, j, k;
char a[5], b[5];
for(i=0; i<n; i++){
scanf("%s %s", a, b);
if(a[0]=='-'){
q[i].ll=-1;
}else{
q[i].ll=a[0]-48;
} if(b[0]=='-'){
q[i].rr=-1;
}else{
q[i].rr=b[0]-48;
}//模拟每一个树节点
}//建树完成
bool f[20];//标记每一个节点是不是儿子
memset(f, false, sizeof(f));
for(i=0; i<n; i++){
if(q[i].ll!=-1){
f[q[i].ll]=true;
}
if(q[i].rr!=-1){
f[q[i].rr]=true;
}
}
int root;
for(i=0; i<n; i++){
if(f[i]==false){
root=i; break;
}
}
//printf("root = %d\n", root); cnt=1;
q[root].dfn=0; q[root].dep=0;
dfs_leaf(root, 0); priority_queue<N>que;
N cur;
for(i=0; i<n; i++){
if(q[i].ll==-1&&q[i].rr==-1){
cur.num=i;
cur.dep=q[i].dep;
cur.dfn=q[i].dfn;
que.push(cur);
//printf("%d节点:深度%d 次序%d\n", i, q[i].dep, q[i].dfn);
}
} bool z=false;
while(!que.empty()){
if(z==false){
printf("%d", que.top().num); z=true; que.pop();
}
else{
printf(" %d", que.top().num); que.pop();
}
}printf("\n");
return 0;
}
 

树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】的更多相关文章

  1. st表、树状数组与线段树 笔记与思路整理

    已更新(2/3):st表.树状数组 st表.树状数组与线段树是三种比较高级的数据结构,大多数操作时间复杂度为O(log n),用来处理一些RMQ问题或类似的数列区间处理问题. 一.ST表(Sparse ...

  2. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

  3. BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树

    [题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...

  4. BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树

    [题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...

  5. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...

  6. BZOJ1901 - Dynamic Rankings(树状数组套主席树)

    题目大意 给定一个有N个数字的序列,然后又m个指令,指令种类只有两种,形式如下: Q l r k 要求你查询区间[l,r]第k小的数是哪个 C i t  要求你把第i个数修改为t 题解 动态的区间第k ...

  7. bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1384  Solved: 629[Submit][Stat ...

  8. [BZOJ 3196] 213平衡树 【线段树套set + 树状数组套线段树】

    题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过 ...

  9. [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】

    题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...

随机推荐

  1. 各种流程图的绘画网路工具 processon

    https://www.processon.com 对应的网址,类似在线viso 很方便使用,工具齐全,推荐使用!

  2. greenlet:轻量级的并发编程

    1 关于greenlet greelet指的是使用一个任务调度器和一些生成器或者协程实现协作式用户空间多线程的一种伪并发机制,即所谓的微线程. greelet机制的主要思想是:生成器函数或者协程函数中 ...

  3. <转> python的垃圾回收机制

    Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...

  4. 【BZOJ1731】[Usaco2005 dec]Layout 排队布局 差分约束

    [BZOJ1731][Usaco2005 dec]Layout 排队布局 Description Like everyone else, cows like to stand close to the ...

  5. Android打印日志管理

    做项目的时候,免不了要打印许多日志,等项目上线了,想要去除日志是又找不到在哪里怎么办?我们可以建立一个日志打印的类来统一管理: public class LogUtil { public static ...

  6. python多线程/多进程

    thread和threading的区别 threading相对与thread是更高级别的线程管理模块 thread和threading模块中的一些属性会有冲突 thread模块拥有的同步原因实际上只有 ...

  7. ehcarts之toolbox,工具栏

    toolbox 工具栏.内置有导出图片,数据视图,动态类型切换,数据区域缩放,重置五个工具. feature各工具配置项.具体显示功能 1.saveAsImage 保存为图片. 2.restore 还 ...

  8. 问题:Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder.

    创建AVD时,发现创建不成功,报错“Unable to find a 'userdata.img' file for ABIarmeabi to copy into the AVD folder.” ...

  9. linux下tcpdump命令的使用

    一般情况下linux系统会自带tcpdump工具,如果系统没有安装,直接用命令安装就行了. 安装命令:yum install -y tcpdump 查看安装版本命令:tcpdump --help 查看 ...

  10. Spring 单例

    我们知道 Web 容器本身就是多线程的,Web 容器为一个 Http 请求创建一个独立的线程,所以由此请求所牵涉到的 Spring 容器中的 Bean 也是运行于多线程的环境下.在绝大多数情况下,Sp ...