HDU 3999 (13.07.07)
来源:互联网 发布:什么美食软件好 编辑:程序博客网 时间:2024/05/16 23:59
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.
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
41 3 4 2
Sample Output
1 3 2 4此题建树的规则在Description中给出按要求建树即可而要求字典序, 用前序遍历即可~ 水!#include<stdio.h>typedef struct Node{ int num; struct Node *l; struct Node *r;}node;node *tree;int isfirst = 1;node* creatTree(node *root, int n) { if(root == NULL) { root = new node; root->num = n; root->l = NULL; root->r = NULL; return root; } else { if(root->num > n) root->l = creatTree(root->l, n); else root->r = creatTree(root->r, n); } return root;}void print(node *root) { if(isfirst) { printf("%d", root->num); isfirst = 0; } else printf(" %d", root->num);}void search(node *root) { if(root != NULL) { print(root); search(root->l); search(root->r); }}int main() { int i, n, date; while(scanf("%d", &n) != EOF) { isfirst = 1; for(i = 0; i < n; i++) { scanf("%d", &date); tree = creatTree(tree, date); } search(tree); printf("\n"); } return 0;}
- HDU 3999 (13.07.07)
- HDU 1175 (13.07.08)
- HDU 3912 (13.07.08)
- HDU 1228 (13.07.13)
- HDU 3732 (13.07.25)
- HDU 4500 (13.07.31)
- HDU 4510 (13.07.31)
- HDU 4508 (13.07.31)
- hdu 3999 BST
- hdu 3999(二叉排序树)
- HDU 3999 BST基础
- HDU 2199 (13.08.07)
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- 如何更改win7任务栏的颜色和桌面背景
- 转:友元函数和友元类
- Android更新界面
- [2010-VLDB] The Performance of MapReduce: An In-depth Study
- android文本自动提示 AutoCompleteTextView的实例2则
- HDU 3999 (13.07.07)
- 润乾报表列统计求和公式
- MySql中把一个表的数据插入到另一个表中的实现代码
- USCAO section 4.1 Fence Loops(最短路,最小环,5级)
- Brackets---卡特兰数
- ACE下串口编程
- MySQL学习足迹记录01--SOURCE,SHOW
- JAVA从零单排2-----流程控制和数组
- [Err] 2013 - Lost connection to MySQL server during query