hdu 3999 二叉查找树
The order of a Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 917 Accepted Submission(s):
496
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.
/*
4
1 3 4 2 1 3 2 4
*/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; struct node
{
int rp;
struct node *lchild;
struct node *rchild;
}; int n,len; void mem(struct node *p)
{
p->rp=;
p->lchild=NULL;
p->rchild=NULL;
}
void insert_ecs(struct node **p,int x)
{
if((*p)==NULL)
{
(*p)=(struct node*)malloc(sizeof(struct node));
mem(*p);
(*p)->rp=x;
return;
}
if( (*p)->rp > x)
insert_ecs( &(*p)->lchild,x);
else insert_ecs( &(*p)->rchild,x);
}
void serch(struct node *p)
{
printf("%d",p->rp);
len++;
if(len!=n) printf(" ",p->rp);
else printf("\n"); if( p->lchild!=NULL )
serch(p->lchild);
if( p->rchild!=NULL )
serch(p->rchild);
}
void deal(struct node *p)
{
if(p->lchild!=NULL)
deal(p->lchild);
if(p->rchild!=NULL)
deal(p->rchild);
free(p);
}
int main()
{
int i,x;
while(scanf("%d",&n)>)
{
struct node *root=NULL;
for(i=;i<=n;i++)
{
scanf("%d",&x);
insert_ecs(&root,x);
}
len=;
serch(root);
free(root);
}
return ;
}
hdu 3999 二叉查找树的更多相关文章
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- HDU 3999 二叉排序树
The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...
- HDU 3999 The order of a Tree
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3999 The order of a Tree 二叉搜索树 BST
建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...
- HDU 3999 The order of a Tree (先序遍历)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4441 Queue Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:对于一个序列,每次有三种操作 insert pos 表示在pos插入一个数,这个数是最小的正数 ...
- 数据结构:二叉查找树(C语言实现)
数据结构:二叉查找树(C语言实现) ►写在前面 关于二叉树的基础知识,请看我的一篇博客:二叉树的链式存储 说明: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 1.若其左子树不空,则左子树上 ...
随机推荐
- IT项目中使用 json格式数据 保存项目配置信息, 在配置文件再读取json文件的内容进行赋值
json格式小巧玲珑,适合做配置文件,特别是大型项目中, 可以将配置信息分类保存到不同的json文件中, 然后再在配置文件中读取配置文件的数据进行赋值, 这里以python为例进行说明: 假设在you ...
- Requests Header | Http Header
Requests Header | Http Header Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accep ...
- TPshop商城 Getshell后门文件分析与复现
本文作者:i春秋签约作家——Laimooc 官网地址:http://www.tp-shop.cn/ 影响版本:TPSHOP v2.0.0 后门文件:eval-stdin.php 内容: <?ph ...
- js中数组的操作方法
今天给大家带来一篇有关数组操作方法的文章. 新建数组 方法一:通过new运算符创建一个数组构造函数. var arr = new Array(); 方法二:通过方括号直接创建直接量数组. var ar ...
- i2c_smbs 函数
i2c_smbus系列函数有: s32 i2c_smbus_read_byte(const struct i2c_client *client); s32 i2c_smbus_write_byte(c ...
- FileRecv VNCViewer 使用方法
版本 区别 一路点点点 . 就ok了 看到这个页面 点击 vnc viewer 输入 老师 会告诉你 IP地址 点击 就ok
- Mac 10.12安装飞鸽传书IPMessager
说明:这个版本的飞鸽传书不能和Linux的互通,但是可以和Windows的互通,我猜测是协议问题:如果想要互通只能是Mac和Linux同时安装iptux. 下载: (链接: https://pan.b ...
- Linux下的Shell特殊符号大全(转)
在shell中常用的特殊符号罗列如下: # ; ;; . , / \ 'string'| ! $ ${} $? $$ $* "string"* ** ? : ^ $# $@ `co ...
- JVectorMap地图插件.Net版开源
jVectorMap地图插件只要浏览器技术JavaScript.CSS,HTML,SVG或VML就可以浏览使用,不需要Flash或其他专有的浏览 器插件.所以jVectorMap在所有现代移动浏览器上 ...
- 连接Git@OSC操作步骤
一.准备工作 软件下载 Git:地址 TortoiseGit:地址 二.安装与配置 1.Git安装 Git配置 设置客户端的用户名和email 然后,到Git@OSC 上面注册一个帐号. 这个帐号就是 ...