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

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
 
Source
 
 
  1. /*
  2. 4
  3. 1 3 4 2
  4.  
  5. 1 3 2 4
  6. */
  7. #include<iostream>
  8. #include<stdio.h>
  9. #include<cstring>
  10. #include<cstdlib>
  11. using namespace std;
  12.  
  13. struct node
  14. {
  15. int rp;
  16. struct node *lchild;
  17. struct node *rchild;
  18. };
  19.  
  20. int n,len;
  21.  
  22. void mem(struct node *p)
  23. {
  24. p->rp=;
  25. p->lchild=NULL;
  26. p->rchild=NULL;
  27. }
  28. void insert_ecs(struct node **p,int x)
  29. {
  30. if((*p)==NULL)
  31. {
  32. (*p)=(struct node*)malloc(sizeof(struct node));
  33. mem(*p);
  34. (*p)->rp=x;
  35. return;
  36. }
  37. if( (*p)->rp > x)
  38. insert_ecs( &(*p)->lchild,x);
  39. else insert_ecs( &(*p)->rchild,x);
  40. }
  41. void serch(struct node *p)
  42. {
  43. printf("%d",p->rp);
  44. len++;
  45. if(len!=n) printf(" ",p->rp);
  46. else printf("\n");
  47.  
  48. if( p->lchild!=NULL )
  49. serch(p->lchild);
  50. if( p->rchild!=NULL )
  51. serch(p->rchild);
  52. }
  53. void deal(struct node *p)
  54. {
  55. if(p->lchild!=NULL)
  56. deal(p->lchild);
  57. if(p->rchild!=NULL)
  58. deal(p->rchild);
  59. free(p);
  60. }
  61. int main()
  62. {
  63. int i,x;
  64. while(scanf("%d",&n)>)
  65. {
  66. struct node *root=NULL;
  67. for(i=;i<=n;i++)
  68. {
  69. scanf("%d",&x);
  70. insert_ecs(&root,x);
  71. }
  72. len=;
  73. serch(root);
  74. free(root);
  75. }
  76. return ;
  77. }
 

hdu 3999 二叉查找树的更多相关文章

  1. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

  2. <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

  3. HDU 3999 二叉排序树

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

  4. 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 ...

  5. HDU 3999 The order of a Tree 二叉搜索树 BST

    建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...

  6. 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 ...

  7. hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. HDU 4441 Queue Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:对于一个序列,每次有三种操作   insert pos  表示在pos插入一个数,这个数是最小的正数 ...

  9. 数据结构:二叉查找树(C语言实现)

    数据结构:二叉查找树(C语言实现) ►写在前面 关于二叉树的基础知识,请看我的一篇博客:二叉树的链式存储 说明: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 1.若其左子树不空,则左子树上 ...

随机推荐

  1. [bzoj1951] [Sdoi2010]古代猪文 费马小定理+Lucas定理+CRT

    Description "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久 ...

  2. tushare模块的应用

    一.简介以及环境安装 TuShare是一个著名的免费.开源的python财经数据接口包.其官网主页为:TuShare -财经数据接口包.该接口包如今提供了大量的金融数据,涵盖了股票.基本面.宏观.新闻 ...

  3. 根据某个key给二维数组分组

    /** * 根据某个key给二维数组分组 */ private function array_group_by($arr, $key) { $grouped = []; foreach ($arr a ...

  4. spring管理hibernate session的问题探究

    我们再用spring管理hibernate的时候, 我们会继承HibernateDaoSupport 或者HibernateTemplate类. 我们不知道这两个类之间有什么关系. 也没有去关闭ses ...

  5. 计算机网络课设之TCP通讯录

    这篇文章我主要是想对这学期末计算机网络课程设计所做的一个小项目也就是基于tcp协议的通讯录做一个总结梳理.项目的具体的代码实现是基于C语言,当然在此之前网上也有一些基于c++编写的tcp通讯录,原理都 ...

  6. TX2 用文件IO的方式操作GPIO

    概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户空间,GPIO 的 ...

  7. Ubuntu16.04安装视觉SLAM环境(OpenCV)

    一.安装依赖库 sudo apt-get install build-essential sudo apt--dev pkg-config libavcodec-dev libavformat-dev ...

  8. createQuery与createSQLQuery区别

    该篇文章也贴上来: hibernate 中createQuery与createSQLQuery两者区别是:前者用的hql语句进行查询,后者可以用sql语句查询前者以hibernate生成的Bean为对 ...

  9. mysql中字符串1.1/1.2/1.2.2/1.2.5排序问题

    1.创建查询函数:(split_pid为函数名称) create function split_pid(str varchar (1000),delimiter varchar(1)) returns ...

  10. [转] 手动上传jar包到远程仓库 (maven deploy)

    [From] https://my.oschina.net/360yg/blog/1588899 前言:通常允许上传的远程仓库有两种:Snapshots和Releases,分别为快照版仓库和稳定版仓库 ...