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. ubuntu在vmware下使用问题

    问题1: 在vmware虚拟机下安装的ubuntu系统,为了在windows和ubuntu之间拷贝数据方便,所以在vmware上安装了vmware tools.在安装了vmware tool之后会在u ...

  2. shell脚本中echo显示内容带颜色显示

    格式如下 : echo -e "\033[41;36m something here \033[0m" 其中41的位置代表底色, 36的位置是代表字的颜色 注: 1.字背景颜色和文 ...

  3. python3 - 动态添加属性以及方法

    给实例动态添加方法,需引入types模块,用其的MethodType(要绑定的方法名,实例对象)来进行绑定:给类绑定属性和方法,可以通过 实例名.方法名(属性名) = 方法名(属性值) 来进行绑定.给 ...

  4. hdu 5185(DP)

    不错的一道dp题目,一开始想了一种N*N的dp,后面就一直想怎么优化,然后就一直都在坑中了. 这题题解还是看早了,应该再多想会的,多换种表示状态的方法再想想. dp[i][j]=dp[i-j][j]+ ...

  5. 【BZOJ2286】[Sdoi2011]消耗战 虚树

    [BZOJ2286][Sdoi2011]消耗战 Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的 ...

  6. 关于VUe的生命周期小小的理解

    实例化初始化->beforeCreate()->数据监测->事件配置->实例已经创建完成(created),在这一步,实例已完成以下的配置:数据观测(data observer ...

  7. JS将秒转换为 天

    function SecondToDate(msd) {             var time =msd             if (null != time && " ...

  8. iOS10通知框架UserNotifications

    在iOS10上,苹果将原来散落在UIKit中各处的用户通知相关的代码进行重构,剥离,打造了一个全新的通知框架-UserNotifications.笔者最近在开发公司通知相关的需求,跟着WWDC2016 ...

  9. 深度学习:Keras入门(二)之卷积神经网络(CNN)(转)

    转自http://www.cnblogs.com/lc1217/p/7324935.html 1.卷积与神经元 1.1 什么是卷积? 简单来说,卷积(或内积)就是一种先把对应位置相乘然后再把结果相加的 ...

  10. 标准c内存函数的使用方法

    标准c内存函数 calloc 语法:     #include <stdlib.h>   void *calloc( size_t num, size_t size ); 功能: 函数返回 ...