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. Git 常用命令 思维导图(转)

    Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. 本来想着只把最有用.最常用的 Git 命令记下来,但是总觉得这个也挺有用.那个也用 ...

  2. 构建高性能服务(二)java高并发锁的3种实现

    构建高性能服务(二)java高并发锁的3种实现 来源:http://www.xymyeah.com/?p=46   提高系统并发吞吐能力是构建高性能服务的重点和难点.通常review代码时看到sync ...

  3. Selenium2(WebDriver)总结(三)---元素定位方法

    元素定位的重要性不言而喻,如果定位不到元素谈何操作元素呢,webdrvier提供了很多种元素定位方法,如ID,Name,xpath,css,tagname等. 例如需要定位如下元素: <inpu ...

  4. Linux中使用GoAccess进行日志实时监控

    一.用法命令: goaccess access_log -o /var/www/html/report.html --real-time-html 说明:请先安装Httpd和Goaccess 二.效果 ...

  5. MySql 常见错误代码大全

    B.. 服务器错误代码和消息 服务器错误信息来自下述源文件: · 错误消息信息列在share/errmsg.txt文件中.“%d”和“%s”分别代表编号和字符串,显示时,它们将被消息值取代. · 错误 ...

  6. Warning: Divide by zero.

    问题:如标题 解决方案:分母加上+eps   参考自:http://www.ilovematlab.cn/thread-43128-1-1.html

  7. [转]Spring MVC之@RequestMapping 详解

    前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, ...

  8. top命令参数

    参数说明 d 指定每两次屏幕信息刷新之间的时间间隔.当然用户可以使用s交互命令来改变之. p 通过指定监控进程ID来仅仅监控某个进程的状态. q该选项将使top没有任何延迟的进行刷新.如果调用程序有超 ...

  9. VS2015使用小技巧

    VS2015常用快捷键 1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键“Ctrl + -”; 2)前进到下一个光标位置:“Ctrl + Shift + - ” 2.复 ...

  10. Oracle SQLPLUS提示符设置

    Oracle SQLPLUS提示符设置 把Oracle sqlplus提示符修改为如下,可以提醒你所在的用户模式,减少误操作. set sqlprompt _user'@'_connect_ident ...