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.若其左子树不空,则左子树上 ...
随机推荐
- 完全卸载mysql数据库教程
转自:https://jingyan.baidu.com/article/f96699bbaa8fc1894f3c1b5a.html 1,控制面板——>所有控制面板项——>程序和功能,卸载 ...
- Python dict转化为string方法
dict-->string: str() string-->dict eval()(这个只是网上看的,没实测)
- C#控制台输出退格实现变换闪烁的字符效果
C#控制台输出退格实现变换闪烁的字符效果,传统的Console.Clear()方法能清除控制台上的所有内容. 如果用 Console.Write('\u0008');可以实现输出退格,这样就可以方便地 ...
- 【表单验证】基于jQuery的高度灵活的表单验证(无UI)
表单验证是前端开发过程中常见的一个需求,产品需求.业务逻辑的不同,表单验证的方式方法也有所区别.而最重要的是我们要清楚,表单验证的核心原则是--错误信息提示准确,并且尽可能少的打扰/干扰用户的输入和体 ...
- [Virtualization] Virtualbox 转换磁盘格式(vboxmanage) disk to VMware
一.需求 今天去参加网络安全的培训,Engineer 分享了一份 Virtual box 虚拟机.但是只有我拷取了这份文件,小伙伴们都是装的 VMware Workstation.那么该如何将vdi转 ...
- vscode 本地调试nodejs
1.首先通过node-inspect插件可以debug nodejs ,先起nodejs服务,再启用node-inpector服务 安装调试器 npm install -g node-inspec ...
- 性能测试 vs 负载测试 vs 压力测试
在做一些软件测试工作时,常常会被提及性能测试.负载测试.压力测试,这也是在软件测试方面最容易混淆的三个概念.之前和一个测试大牛聊天,他和我说常常面试一些测试人员会问一些这样的问题,大多人认为负载测试等 ...
- mysql误删root用户
在操作mysql时误删除root用户,如何进行恢复 在安装hive时候误删除root用户,存在的用户没有权限,解决方法如下. 1.首先在/etc/my.cnf文件中 mysqlID下面添加 skip- ...
- 【笔记】AJAX+SweetAlert插件实现删除操作
[笔记]AJAX+SweetAlert插件实现删除操作 Django AJAX SweetAlert 展示 SweetAlert 插件介绍 SweetAlert 是一个 JS 插件,能够完美替代 J ...
- 最新版IntelliJ IDEA2019.1破解教程(2019.04.08更新)
[原文链接]:https://www.tecchen.xyz/idea-crack.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力有限,对文章 ...