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. Java Web 生成临时文件并下载(原)

    概述:本文是  java 服务器端生成文件并下载的示例,并不完善,下载之后一般来说还需要删除临时文件. 注意:临时文件存放在 /WEB-INF/tmp 目录下,所以先要把  tmp 目录建起来. pu ...

  2. JAVA-SpringMVC开发第一个应用

    找到eclipse工具路径 打开eclipse.exe 选择workspace的存放位置,点击ok 点击file-new 选择web-dynamic web project(动态web项目)-next ...

  3. php命名空间的使用,同一个命名空间可以在多个文件中定义

    php namespace的使用,直接打印出已经定义的命名空间 直接上代码,a.php , b.php, c.php , main.php a.php <?php namespace A{ cl ...

  4. wdcp下nginx+apache混合模式的主机配置

    /www/wdlinux/httpd-2.2.22/conf/vhost/xxx.xxx.com.conf <VirtualHost *:88>DocumentRoot /www/web/ ...

  5. android中XRecyclerView控件的使用

    控件的地址:https://github.com/XRecyclerView/XRecyclerView XRecyclerView控件是一个加强版的RecyclerView,可以很方便的实现下拉刷新 ...

  6. Android中创建PopupMenu弹出式菜单

    之前写过一篇创建option menu的文章:Android中创建option menu 本文主要是讲如何创建PopupMenu弹出式菜单 1.首先创建menu文件menu2.xml: <?xm ...

  7. Java多线程-BlockingQueue-ArrayBlockingQueue-LinkedBlockingQueue

    前言: BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利.本文详细介绍了Blocking ...

  8. sublime text 3中文乱码问题解决的方法

    一.首先要确保本机sublime已经有安装包管理器,假设没有.安装方法:http://blog.chinaunix.net/uid-12014716-id-4269991.html 文中的第一步:安装 ...

  9. hibernate调用mysql存储过程

    在mysql中创建两个存储过程,如下: 1.根据id查找某条数据: )) begin select * from emp where empId=id; end; 2.根据id查找某个字段,并返回 ) ...

  10. Java 希尔排序

    效率:O(n*logN) package sort; import utils.Util; /** * 希尔排序 * 以h为间隔,进行比較. 按一定公式.先求出最大的间隔h * 当h值大时,须要移动的 ...