树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】
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 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】的更多相关文章
- st表、树状数组与线段树 笔记与思路整理
已更新(2/3):st表.树状数组 st表.树状数组与线段树是三种比较高级的数据结构,大多数操作时间复杂度为O(log n),用来处理一些RMQ问题或类似的数列区间处理问题. 一.ST表(Sparse ...
- bzoj1901--树状数组套主席树
树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...
- BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树
[题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...
- BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树
[题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...
- BZOJ1901 - Dynamic Rankings(树状数组套主席树)
题目大意 给定一个有N个数字的序列,然后又m个指令,指令种类只有两种,形式如下: Q l r k 要求你查询区间[l,r]第k小的数是哪个 C i t 要求你把第i个数修改为t 题解 动态的区间第k ...
- bzoj 3110: [Zjoi2013]K大数查询 树状数组套线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1384 Solved: 629[Submit][Stat ...
- [BZOJ 3196] 213平衡树 【线段树套set + 树状数组套线段树】
题目链接:BZOJ - 3196 题目分析 区间Kth和区间Rank用树状数组套线段树实现,区间前驱后继用线段树套set实现. 为了节省空间,需要离线,先离散化,这样需要的数组大小可以小一些,可以卡过 ...
- [BZOJ 1901] Dynamic Rankings 【树状数组套线段树 || 线段树套线段树】
题目链接:BZOJ - 1901 题目分析 树状数组套线段树或线段树套线段树都可以解决这道题. 第一层是区间,第二层是权值. 空间复杂度和时间复杂度均为 O(n log^2 n). 线段树比树状数组麻 ...
随机推荐
- OpenCV学习笔记二:OpenCV模块一览
注:本系列博客基于OpenCV 2.9.0.0 一,一览图: 二,模块: /* 基础库 */ 1,opencv_core(链接) ,opencv最基础的库.包含exception,point,rect ...
- openssl 升级 操作 -1
好多公司都会用绿盟扫描系统漏洞,里边就会涉及到ssl 漏洞,原因是openssl 版本低导致,会让你升级到指定版本.下面就介绍一下openssl 版本升级的操作方案. 一. 查看系统版本 [root@ ...
- 图解Microsoft SQL Server——“远程过程调用失败 [0x800706be] 错误“。
今天在配置SqlServer启动参数时,发现"开始菜单"->“配置工具”->“SQL Server 配置管理器”中的“SQL Server 服务”一项,右边栏里显示的是 ...
- boost::lockfree::stack
#include <boost/thread/thread.hpp> #include <boost/lockfree/stack.hpp> #include <iost ...
- 【BZOJ1280】Emmy卖猪pigs 最大流
[BZOJ1280]Emmy卖猪pigs Description Emmy在一个养猪场工作.这个养猪场有M个锁着的猪圈,但Emmy并没有钥匙.顾客会到养猪场来买猪,一个接着一个.每一位顾客都会有一些猪 ...
- <2013 10 18> Linux随机数生成器可能确实存在安全弱点
生成随机数是密码学中的一项基本任务,是生成加密密钥和加密算法或加密协议所必不可少的.理想中的随机数是完美的,但实践中的随机数都是用伪随机数生成器(PRNG)生成,随机数的质量对安全性至关重要.安全专家 ...
- Arcgis server服务启动后停止
---恢复内容开始--- 重新按装server10.1,10sp1和10.2 易出现服务无法启动.在管理中手动启动服务,出现如图1所示的错误. 图1 错误信息 本人多次遇到用户反馈这问题,最初以为是因 ...
- 使用jquery的ajax方法获取下拉列表值
AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新,用户体验非常好. ...
- 如何使用 Opencv dnn 模块调用 Caffe 预训练模型?
QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt"; QString mo ...
- MySQL 中有关auto_increment及auto_increment_offset方面的介绍
数据库查询中,涉及到auto_increment中的参数变量一共有两个 [root@localhost][(none)]> show variables like 'auto_inc%'; +- ...
样例建树后的样子