博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hdu - 3999- The order of a Tree
阅读量:4698 次
发布时间:2019-06-09

本文共 4027 字,大约阅读时间需要 13 分钟。

先上题目

  

The order of a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 830    Accepted Submission(s): 448

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
 
 
 
  级别是水题,可是还是wa了一次,还是明知道会wa的还是交了上去,题目的意思就是给你一棵二叉排序树的输入顺序,求出这棵树的先序遍历。
  这一题用指针来做的话很简单,但是我一开始用数组,而且知道2^100000一定会爆,但是还是用了数组来做= =,好吧我手贱。然后改成指针来做以后就过了,用时125MS。随后经人提醒用静态链表做,于是花了大概10分钟写出来了一个静态链表版的(好像更简单了= =),交上去1y,用时95MS,看来静态链表是个好东西啊,而且听说java没有指针,只可以用静态链表= =。
 
上代码
 
 
1 #include 
2 #include
3 #include
4 #include
5 #define MAX 100000+10 6 using namespace std; 7 8 int d[MAX],l[MAX],r[MAX]; 9 10 void insert(int e)11 {12 int i;13 if(d[0]==1) {d[d[0]++]=e;;return;}14 i=1;15 while(1)16 {17 if(d[i]>e)18 {19 if(l[i]==0) {d[d[0]]=e;l[i]=d[0]++;return;}20 else i=l[i];21 }22 else23 {24 if(r[i]==0) {d[d[0]]=e;r[i]=d[0]++;return;}25 else i=r[i];26 }27 }28 }29 30 void load()31 {32 int i,count;33 stack
s;34 if(d[0]==1) return ;35 i=1;36 s.push(i);37 count=0;38 while(!s.empty())39 {40 i=s.top();41 s.pop();42 if(count++) printf(" ");43 printf("%d",d[i]);44 if(r[i]) s.push(r[i]);45 if(l[i]) s.push(l[i]);46 }47 }48 49 int main()50 {51 int i,n,e;52 while(scanf("%d",&n)!=EOF)53 {54 memset(d,0,sizeof(d));55 memset(l,0,sizeof(l));56 memset(r,0,sizeof(r));57 d[0]=1;58 for(i=0;i
静态链表版

 

 

1 #include 
2 #include
3 #include
4 #include
5 #define MAX 100000+10 6 using namespace std; 7 8 typedef struct node 9 {10 int d;11 struct node* l,*r;12 }node;13 14 node *p;15 16 void insert(node *f)17 {18 node *root;19 root=p;20 if(p==NULL) p=f;21 else22 {23 while(1)24 {25 if(root->d > f->d)26 {
if(root->l)root=root->l;else {root->l=f;return;}}27 else28 {
if(root->r)root=root->r;else {root->r=f;return;}}29 }30 }31 }32 33 void load()34 {35 int count;36 node* f;37 stack
s;38 f=p;39 count=0;40 s.push(f);41 while(!s.empty())42 {43 f=s.top();44 if(count++) printf(" ");45 printf("%d",f->d);46 s.pop();47 if(f->r) s.push(f->r);48 if(f->l) s.push(f->l);49 free(f);50 }51 }52 53 int main()54 {55 int i,n,e,count;56 while(scanf("%d",&n)!=EOF)57 {58 node* f;59 p=NULL;60 for(i=0;i
d=e;65 f->l=f->r=NULL;66 insert(f);67 }68 count=0;69 load();70 printf("\n");71 }72 return 0;73 }
指针版

 

转载于:https://www.cnblogs.com/sineatos/p/3189684.html

你可能感兴趣的文章
初级ant的学习
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>
不把DB放进容器的理由
查看>>
OnePage收集
查看>>
Java parseInt()方法
查看>>
yahoo的30条优化规则
查看>>
[CCF2015.09]题解
查看>>
[NYIST15]括号匹配(二)(区间dp)
查看>>
json_value.cpp : fatal error C1083: 无法打开编译器生成的文件:No such file or directory
查看>>
洛谷 P1101 单词方阵
查看>>
Swift DispatchQueue
查看>>
C#和JAVA 访问修饰符
查看>>
小甲鱼OD学习第1讲
查看>>
HDU-1085 Holding Bin-Laden Captive-母函数
查看>>