HDU_3999_二叉排序树
The order of a Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1749 Accepted Submission(s):
908
related to the order of keys we insert. To be precisely:
1. insert a key k
to a empty tree, then the tree become a tree with
only one
node;
2. insert a key k to a nonempty tree, if k is less than the root
,insert
it to the left sub-tree;else insert k to the right sub-tree.
We
call the order of keys we insert “the order of a tree”,your task is,given a oder
of a tree, find the order of a tree with the least lexicographic order that
generate the same tree.Two trees are the same if and only if they have the same
shape.
first line of each testcase is an integer n(n <= 100,000),represent the
number of nodes.The second line has n intergers,k1 to kn,represent the order of
a tree.To make if more simple, k1 to kn is a sequence of 1 to n.
tree that generate the same tree with the least lexicographic.
1 3 4 2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; int lson[],rson[],n; void Insert(int rt,int x)
{
if(rt>x)
{
if(lson[rt]==)
lson[rt]=x;
else
Insert(lson[rt],x);
}
else
{
if(rson[rt]==)
rson[rt]=x;
else
Insert(rson[rt],x);
} } int cnt=;
void order(int rt) //深搜
{
if(rt==)
return;
printf("%d",rt);
cnt++;
if(cnt==n)
printf("\n");
else
printf(" ");
order(lson[rt]);
order(rson[rt]);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
cnt=;
int num,root;
memset(lson,,sizeof(lson));
memset(rson,,sizeof(rson));
scanf("%d",&root);
for(int i=; i<n; i++)
{
scanf("%d",&num);
Insert(root,num);
}
order(root);
//cout<<n<<" "<<cnt<<endl;
}
return ;
}
HDU_3999_二叉排序树的更多相关文章
- 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较
二分法查找: 『在有序数组的基础上通过折半方法不断缩小查找范围,直至命中或者查询失败.』 二分法的存储要求:要求顺序存储,以便于根据下标随机访问 二分法的时间效率:O(Log(n)) 二分 ...
- 二叉排序树(BST)创建,删除,查找操作
binary search tree,中文翻译为二叉搜索树.二叉查找树或者二叉排序树.简称为BST 一:二叉搜索树的定义 他的定义与树的定义是类似的,也是一个递归的定义: 1.要么是一棵空树 2.如果 ...
- 数据结构图文解析之:树的简介及二叉排序树C++模板实现.
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- 二叉树建立,遍历和二叉排序树的判断【c++】
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
- 二叉排序树(Binary Sort Tree)
参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...
- HDU 3999 二叉排序树
The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...
- 二叉排序树(BST)的建立
给一个非递归的吧. /* 已知,二叉树存储结构定义见bstree.h,请编写一个算法函数bstree creatBstree(int a[],int n), 以数组a中的数据作为输入建立一棵二叉排序树 ...
- POJ 2418 各种二叉排序树
题意很明确,统计各个字符串所占总串数的百分比,暴力的话肯定超时,看了书上的题解后发现这题主要是用二叉排序树来做,下面附上n种树的代码. 简单的二叉排序树,不作任何优化(C语言版的): #include ...
随机推荐
- CentOS 7.3降低内核版本为7.2
查看当前内核版本: [root@nineep ~]# uname -r 2.3.10.0-514.2.2.el7.x86_64 查看当前发行版本: [root@nineep ~]# cat /etc ...
- noip模拟赛 gcd
题目更正:输出的a<b. 分析:这是一道数学题,范围这么大肯定是有规律的,打个表可以发现f(a,b)=k,a+b最小的a,b是斐波那契数列的第k+1项和k+2项.矩阵快速幂搞一搞就好了. #in ...
- HBase行键的设计
rowkey是行的主键,而且hbase只能用rowkey范围即scan来查找数据.rowkey是以字典排序的.可以巧妙设计行键,比如想通过电影的评价进行排序,可以把评分rate和电影id组合起来,ra ...
- ant生成war包的简单实现
按网上的操作,实现一下. build.xml: <?xml version="1.0" ?> <project name ="antwebproject ...
- data guard 的redo 传输
data guard 通过把redo从primary数据库传输到standby数据库并应用在standby数据库来实现自己的功能. redo 传输是有2种模式 1. 同步 sync 2. 异步 asy ...
- 【面试】【Spring常见问题总结】【07】
[常见面试问题总结文件夹>>>] 61.Spring IoC容器的依赖有两层含义: Bean依赖容器:也就是说Bean要依赖于容器,这里的依赖是指容器负责创建Bean并管理Bean的 ...
- mysql高可用架构方案之中的一个(keepalived+主主双活)
Mysql双主双活+keepalived实现高可用 文件夹 1.前言... 4 2.方案... 4 2.1.环境及软件... 4 2.2.IP规划... 4 2.3.架构图... ...
- C语言之基本算法40—字符串删除元音字母倒序输出
//字符串,数组 /* ================================================================== 题目: 输入一行字符,将辅音字母按反序输出 ...
- 【iOS】网络载入图片缓存与SDWebImage
载入网络图片能够说是网络应用中必备的.假设单纯的去下载图片,而不去做多线程.缓存等技术去优化,载入图片时的效果与用户体验就会非常差. 一.自己实现载入图片的方法 tips: *iOS中全部网络訪问都是 ...
- HTML文档基础
一.HTML(Hyper Text Markup Language超文本标记语言)是一种用来制作超文本文档的简单标记语言,HTML在正文的文本中编写各种标记,通过Web浏览器进行编译和运行才干正确显示 ...