Hdu

先上题目

  

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 <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <stack>
 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         else
23         {
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<int> 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<n;i++)
59         {
60             scanf("%d",&e);
61             insert(e);
62         }
63         load();
64         printf("
");
65     }
66     return 0;
67 }
静态链表版
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <stack>
 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     else
22     {
23         while(1)
24         {
25             if(root->d > f->d)
26             {if(root->l)root=root->l;else {root->l=f;return;}}
27             else
28             {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<node*> 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<n;i++)
61         {
62             scanf("%d",&e);
63             f=(node*)malloc(sizeof(node));
64             f->d=e;
65             f->l=f->r=NULL;
66             insert(f);
67         }
68         count=0;
69         load();
70         printf("
");
71     }
72     return 0;
73 }
指针版
原文地址:https://www.cnblogs.com/sineatos/p/3189684.html