The order of a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 835    Accepted Submission(s): 453

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
 
Recommend
lcy
 

题意是给你一个序列建立一棵二叉搜索树 要你找出另外一个序列可以建立和原序列建立的二叉搜索树一样且这个序列是字典序最小,一看根据二叉搜索树的特点 左孩子小 右孩子大 直接输出一个先序遍历就OK!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<cstdlib> using namespace std; const int N=; struct Tree{
Tree *l,*r;
int x;
}tree; Tree *root; Tree *Create(Tree *rt,int x){
if(rt==NULL){
rt=(Tree *)malloc(sizeof(Tree));
rt->x=x;
rt->l=rt->r=NULL;
return rt;
}
if(rt->x>x) //insert a key k to a nonempty tree, if k is less than the root ,insert it to the left sub-tree
rt->l=Create(rt->l,x);
else //else insert k to the right sub-tree
rt->r=Create(rt->r,x);
return rt;
} void PreOrder(Tree *rt,int x){ //先序历遍
if(x==)
printf("%d",rt->x);
else
printf(" %d",rt->x);
if(rt->l!=NULL)
PreOrder(rt->l,);
if(rt->r!=NULL)
PreOrder(rt->r,);
} int main(){ //freopen("input.txt","r",stdin); int n,x;
while(~scanf("%d",&n)){
root=NULL;
for(int i=;i<n;i++){
scanf("%d",&x);
root=Create(root,x);
}
PreOrder(root,);
printf("\n");
}
return ;
}

HDU 3999 The order of a Tree (先序遍历)的更多相关文章

  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

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

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

  5. hdu3999-The order of a Tree (二叉树的先序遍历)

    http://acm.hdu.edu.cn/showproblem.php?pid=3999 The order of a Tree Time Limit: 2000/1000 MS (Java/Ot ...

  6. HDU 3999 二叉排序树

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

  7. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  8. hdu3999The order of a Tree (二叉平衡树(AVL))

    Problem Description As we know,the shape of a binary search tree is greatly related to the order of ...

  9. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

随机推荐

  1. Deep Learning 教程(斯坦福深度学习研究团队)

    http://www.zhizihua.com/blog/post/602.html 说明:本教程将阐述无监督特征学习和深度学习的主要观点.通过学习,你也将实现多个功能学习/深度学习算法,能看到它们为 ...

  2. mysql的sql分页函数limit使用 (转)

    http://www.cnblogs.com/beijingstruggle/p/5631603.html mysql的sql分页函数limit使用 My sql数据库最简单,是利用mysql的LIM ...

  3. Spring(十五):通过注解配置 Bean

    在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...

  4. (转)Unity3D研究院之Assetbundle的原理(六十一)

    Assetbundle 是Unity Pro提供提供的功能,它可以把多个游戏对象或者资源二进制文件封装到Assetbundle中,提供了封装与解包的方法使用起来很便利. 1.预设          A ...

  5. 让你的Python代码更加pythonic

    http://wuzhiwei.net/be_pythonic/ 何为pythonic? pythonic如果翻译成中文的话就是很python.很+名词结构的用法在中国不少,比如:很娘,很国足,很CC ...

  6. Spring 集成 redistemplate

    jar包 <version.clients.jedis>2.7.2</version.clients.jedis><version.data.redis>1.6.2 ...

  7. Linux中使用sed命令或awk命令修改常规配置文件

    一.方案: Linux中使用sed命令或awk命令修改常规配置文件 二.步骤: 1.假设有一个a.txt,内容如下: #!/bin/bash aa= bbb= ccc= #ddd= 2.如果想要把里面 ...

  8. Python 字典(联合内存、联合数组)

    字典 Python有一个内建数据类型是字典(Dictionaries).字典在某些语言中可能称为“联合内存”("associative memories'')或“联合数组”("as ...

  9. python binascii模块详解

    ['Error', 'Incomplete', 'b2a_hex', 'hexlify' #Hexadecimal representation of binary data. 字符串转16进制'a2 ...

  10. Searching for equivalent of FileNotFoundError in Python 2

    I created a class named Options. It works fine but not not with Python 2. And I want it to work on b ...