树 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). 线段树比树状数组麻 ...
随机推荐
- Laravel5.1 数据库--DB运行原生SQL
Laravel操作数据库有三种:DB原生SQL.构建器.Model.这三种依情况而决定使用哪种更合适. 那么今儿咱就从DB原生SQL说起: 1 用DB门面原生SQL语句操作 用DB门面操作的话呢 无非 ...
- <jsp:include page="xxxx"/> <@page include="xxxx"@>
<jsp:include page="/inc/all/header.html" flush="true"/> 动态导入页面 1. 基本上没遇到 ...
- 队列实现 (双向循环链表 C++)
队列是非常easy的.可是用数组实现可能更好点. . (事实上我认为数组在多个队列的时候更难) 然后我是第一次写双向循环链表.指向太乱了. 我这里是依照自己的想法.建立了一个头节点,一个尾节点,然后依 ...
- LINUX手动查看和修改MTU值的方法
默认的ANDROID系统中不带BUSYBOX,所以无法用BUSYBOX IFCONFIG来查看修改MTU值.其实网卡的MTU值是保存在/sys/class/net/eth0/mtu文件中,所以可以通过 ...
- JSON Extractor/jp@gc - JSON Path Extractor 举例2
测试描述 使用json返回结果做校验 测试步骤 1.配置http请求 2.根据结果树返回的json,取值 { "status_code":200, "message&qu ...
- sqlserver 脚本和批处理指令小结
一.脚本基础 1.USE语句 设置当前数据库. 2.声明变量 语法:DECLARE @变量名 变量类型 在声明变量后,给变量赋值之 ...
- Hibernate 检索(查询)策略
1.立即检索 (查询) 立即检索就是在调用get方法的时候,会直接向数据库发出sql语句查询,并将结果放到session缓存中,在查询的时候会将级联的对象一对查出,(发出多条sql语句): 明显的缺点 ...
- jquery.fileDownload plugin: Success msg alert before actual pdf download completed
Currently , I use jquery fileDownload plugin to download multiple pdf that in a list page, which eve ...
- 洛谷 P3393 逃离僵尸岛
洛谷 这道题目其实是最短路裸题. 首先看到题目,要求的到"被占点"距离不大于S的点,自然想到了以"被占点"为源点,求一遍最短路,处理出"危险点&quo ...
- C++学习之旅get、getline的使用方法
C++学习之旅get.getline的使用方法 面向行的输入:cin.getline(). 该函数读取整行.它使用通过回车键输入的换行符来确定输入结尾.要调用这样的方法,能够使用cin.getline ...
样例建树后的样子