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 ...
随机推荐
- PowerDesigner16.5 使用遇到的问题
目录 1.PowerDesigner 菜单栏中没有 Datebase 2.PowerDesigner连接远程mysql数据库连接不上 3.怎样去掉窗口中的黑色网格线 4.设置默认"不为空&q ...
- [cogs396] [网络流24题#4] 魔术球 [网络流,最大流,最小路径覆盖]
本题枚举每多一个球需要多少个柱子,可以边加边边计算,每次只需要判断$i-Dinic()$即可:特别注意边界. #include <iostream> #include <algori ...
- Java开发中的几种对象的说明(PO,VO,DTO,BO,POJO,DAO,SAO等)
一.PO :(persistant object ),持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是不错的选择. 二.VO :(value object) ...
- iOS:去除UITableView的空白行
要去除UITableView在运行时显示的多余空白行,只需要将TableView的Style从Plain改为Grouped即可.
- CSDN处理问题神速,顶你,为你点32个赞!
今天10点左右发表了一篇文章,发表之后.文章状态待审核,博文首页不能显示文章,例如以下图所看到的: 于是果断给官网发了第一封Email.10点19分.CSDN给予回复,内容例如以下: 尊敬的用户您好: ...
- TTS-零基础入门之停止列表中单条语音播报
做了一个语音 循环播报列表信息.当我新删除了一天列表之后,发现它仅仅有在下一轮播报中才会取消.这明显是不合理的. 一開始的代码是这样写的. <span style="font-fami ...
- 严格符合CommonJS规范的包特性
严格符合CommonJS规范的包应该具备下面特性: 1.package.json必须在包的顶层文件夹下. 2.二进制文件应该在bin文件夹下. 3.JavaScript代码应该在lib文件夹下. 4. ...
- C# datagridview 删除行(转 学会、放弃博客)
原文引入:http://zhangyanyansy.blog.163.com/blog/static/13530509720106171252978/ datagridview 删除行 2010-07 ...
- SVM中的线性分类器
线性分类器: 首先给出一个非常非常简单的分类问题(线性可分),我们要用一条直线,将下图中黑色的点和白色的点分开,很显然,图上的这条直线就是我们要求的直线之一(可以有无数条这样的直线) 假如说, ...
- Arranging Your Team
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=35800#problem/D #include <iostream> #inc ...