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

Problem Description
As we know,the shape of a binary search tree is greatly
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.
 
Input
There are multiple test cases in an input file. The
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.
 
Output
One line with n intergers, which are the order of a
tree that generate the same tree with the least lexicographic.
 
Sample Input
4

1 3 4 2

 
Sample Output
1 3 2 4
 
给一个序列,依照序列建二叉排序树,输出建成这棵树的最小字典序列。
不能使用线段树那种表示方式来表示树,因为可能会退化为一条链,这时会溢出。
所以用lson和rson两个数组分别存每个结点的左右儿子(题中规定,数字为1—n)。
 
#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_二叉排序树的更多相关文章

  1. 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较

    二分法查找: 『在有序数组的基础上通过折半方法不断缩小查找范围,直至命中或者查询失败.』   二分法的存储要求:要求顺序存储,以便于根据下标随机访问   二分法的时间效率:O(Log(n))   二分 ...

  2. 二叉排序树(BST)创建,删除,查找操作

    binary search tree,中文翻译为二叉搜索树.二叉查找树或者二叉排序树.简称为BST 一:二叉搜索树的定义 他的定义与树的定义是类似的,也是一个递归的定义: 1.要么是一棵空树 2.如果 ...

  3. 数据结构图文解析之:树的简介及二叉排序树C++模板实现.

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  4. 二叉树建立,遍历和二叉排序树的判断【c++】

    // test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  5. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

  6. 二叉排序树(Binary Sort Tree)

    参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...

  7. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...

  8. 二叉排序树(BST)的建立

    给一个非递归的吧. /* 已知,二叉树存储结构定义见bstree.h,请编写一个算法函数bstree creatBstree(int a[],int n), 以数组a中的数据作为输入建立一棵二叉排序树 ...

  9. POJ 2418 各种二叉排序树

    题意很明确,统计各个字符串所占总串数的百分比,暴力的话肯定超时,看了书上的题解后发现这题主要是用二叉排序树来做,下面附上n种树的代码. 简单的二叉排序树,不作任何优化(C语言版的): #include ...

随机推荐

  1. ubuntu16.04安装jdk/mysql/tomcat (使用apt-get命令)

    安装jdk 更新系统安装包缓存,并且安装OpenJDK8 sudo apt-get update sudo apt-get install openjdk-8-jdk 检查jdk版本 java -ve ...

  2. RestEasy用户指南---第6章.@QueryParam

    转载说明出处:http://blog.csdn.net/nndtdx/article/details/6870391 原文地址 http://docs.jboss.org/resteasy/docs/ ...

  3. hdu 4171 最短路

    #include<stdio.h> #include<string.h> #include<queue> #include<iostream> usin ...

  4. CF #328div2 D

    这题不难,当时想出来了,可是却写不出来~~ 现在慢慢写回来,也写得好挫~ 可以知道,被攻击的城市必定可以组成一棵树,然后,传送到的点必定也是城市之一.如果出发后回到原点,则需要2E,E是树的边数,则2 ...

  5. NGINX Web Server Nginx web server

    原文地址:http://nginx.com/resources/admin-guide/web-server/ NGINX Web Server Nginx web server This secti ...

  6. 设计模式实例(Lua)笔记之五(Bridge模式)

    1.描写叙述 今天我要说说我自己,梦想中的我自己,我身价过亿,有两个大公司,一个是房地产公司,一个是服装制造业,这两个公司都非常赚钱,天天帮我在累加財富,事实上是什么公司我倒是不关心,我关心的是是不是 ...

  7. OpenCV 学习(计算图像的直方图)

    OpenCV 计算图像的直方图 计算图像的直方图是图像处理领域一个非经常见的基本操作. OpenCV 中提供了 calcHist 函数来计算图像直方图.只是这个函数说实话挺难用的,研究了好久才掌握了些 ...

  8. Git版本号控制:Git分支处理

    http://blog.csdn.net/pipisorry/article/details/46958699分支的意义创建分支能够避免提交代码后对主分支的影响,同一时候也使你有了相对独立的开发环境. ...

  9. oc37--类工厂方法

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int ag ...

  10. NYOJ skiing(DFS+记忆化搜索)

    skiing                                                                        时间限制:3000 ms  |  内存限制: ...