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. bupt summer training for 16 #7 ——搜索与DP

    https://vjudge.net/contest/174962#overview A.我们发现重点在于x,y只要累加就ok了 在每个x上只有上下两种状态,所以可以记忆化搜索 f[0/1][i]表示 ...

  2. 权限问题导致无法删除ftp文件

    首先吐槽一下,使用新版编辑器,发了两遍愣是time out,果断放弃 这个文章也是一件小事,大致说一下: 有一个java操作ftp文件的程序,运行删除时,总是返回false,也没有报错.開始考虑是没有 ...

  3. 【Java集合源代码剖析】Hashtable源代码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/36191279 Hashtable简单介绍 Hashtable相同是基于哈希表实现的,相同每 ...

  4. cocos2d-html5开发之本地数据存储

    做游戏时常常须要的一个功能呢就是数据的保存了,比方游戏最高分.得到的金币数.物品的数量等等.cocos2d-html5使用了html5.所以html5的数据保存方法是对引擎可用的: html5本地数据 ...

  5. jQuery - 点击图片加边框

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. luogu3834 【模板】可持久化线段树1(主席树)

    关键字:线段树 可持久化 线段树:当版本(即对应的原序列的区间[1,r])一定时,每个节点的left,right下标为值域,值为其对应的原序列区间[1,r]中元素大小在值域中的元素个数. 可持久化:新 ...

  7. ubuntu下C程序的编辑、编译、运行

    均以hello world程序为例 一.vim hello.c/hello.cpp 创建hello.c/hello.cpp文件,并进入vim界面 二.此时按键盘上的很多键均不会有任何反应,键入i,进入 ...

  8. hdoj--1027--Ignatius and the Princess II(dfs)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  9. 【BZOJ 1370】 团伙

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1370 [算法] 并查集 + 拆点 [代码] #include<bits/std ...

  10. 洛谷 P3959 NOIP2017 宝藏 —— 状压搜索

    题目:https://www.luogu.org/problemnew/show/P3959 搜索: 不是记忆化,而是剪枝: 邻接矩阵存边即可,因为显然没有那么多边. 代码如下: #include&l ...