Hdu - 3999- The order of a Tree
2013-07-14 17:29
471 查看
先上题目
Total Submission(s): 830 Accepted Submission(s): 448
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.
[align=left]Sample Input[/align]
4
1 3 4 2
[align=left]Sample Output[/align]
1 3 2 4
级别是水题,可是还是wa了一次,还是明知道会wa的还是交了上去,题目的意思就是给你一棵二叉排序树的输入顺序,求出这棵树的先序遍历。
这一题用指针来做的话很简单,但是我一开始用数组,而且知道2^100000一定会爆,但是还是用了数组来做= =,好吧我手贱。然后改成指针来做以后就过了,用时125MS。随后经人提醒用静态链表做,于是花了大概10分钟写出来了一个静态链表版的(好像更简单了= =),交上去1y,用时95MS,看来静态链表是个好东西啊,而且听说java没有指针,只可以用静态链表= =。
上代码
指针版
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
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.
[align=left]Sample Input[/align]
4
1 3 4 2
[align=left]Sample Output[/align]
1 3 2 4
级别是水题,可是还是wa了一次,还是明知道会wa的还是交了上去,题目的意思就是给你一棵二叉排序树的输入顺序,求出这棵树的先序遍历。
这一题用指针来做的话很简单,但是我一开始用数组,而且知道2^100000一定会爆,但是还是用了数组来做= =,好吧我手贱。然后改成指针来做以后就过了,用时125MS。随后经人提醒用静态链表做,于是花了大概10分钟写出来了一个静态链表版的(好像更简单了= =),交上去1y,用时95MS,看来静态链表是个好东西啊,而且听说java没有指针,只可以用静态链表= =。
上代码
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stack> #define MAX 100000+10 using namespace std; typedef struct node { int d; struct node* l,*r; }node; node *p; void insert(node *f) { node *root; root=p; if(p==NULL) p=f; else { while(1) { if(root->d > f->d) {if(root->l)root=root->l;else {root->l=f;return;}} else {if(root->r)root=root->r;else {root->r=f;return;}} } } } void load() { int count; node* f; stack<node*> s; f=p; count=0; s.push(f); while(!s.empty()) { f=s.top(); if(count++) printf(" "); printf("%d",f->d); s.pop(); if(f->r) s.push(f->r); if(f->l) s.push(f->l); free(f); } } int main() { int i,n,e,count; while(scanf("%d",&n)!=EOF) { node* f; p=NULL; for(i=0;i<n;i++) { scanf("%d",&e); f=(node*)malloc(sizeof(node)); f->d=e; f->l=f->r=NULL; insert(f); } count=0; load(); printf("\n"); } return 0; }
指针版
相关文章推荐
- hdu 3999 The order of a Tree
- The order of a Tree HDU - 3999
- HDU 3999 The order of a Tree
- HDU 3999 The order of a Tree
- hdu 3999 The order of a Tree(二叉搜索树)
- hdu 3999:The order of a Tree
- HDU 3999 The order of a Tree 二叉搜索树 BST
- HDU 3999 The order of a Tree 二叉搜索树 BST
- hdu 3999 The order of a Tree
- HDU-3999 The order of a Tree 树的遍历
- hdu 3999 The order of a Tree
- HDU3999 The order of a Tree(构建二叉搜索树模板指针)
- hdu 3999 The order of a Tree
- Hdu 3999 - The order of a Tree
- HDU 3999----The order of a Tree(二叉树的前序遍历)
- hdu 3999 The order of a Tree
- hdu 3999 The order of a Tree
- 二叉搜索树的前序遍历:hdu 3999 The order of a Tree
- HDU(3999)The order of a Tree
- 多效第16场 HDU 3999 The order of a Tree(二叉树)