The order of a Tree

Problem Description
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
 
二叉排序树如何建立和遍历是关键。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int ld[],rd[],a,num,root,i;
void build(int root,int al)
{
if(al>root)
{
if(rd[root]==-)
{
rd[root]=al;
//cout<<"al:"<<al<<" r root:"<<root<<endl;
}
else build(rd[root],al);
}
else
{
if(ld[root]==-)
{
ld[root]=al;
//cout<<"al:"<<al<<" l root:"<<root<<endl;
}
else build(ld[root],al);
}
} void solve(int root)
{
if(ld[root]!=-)
{
cout<<" "<<ld[root];
solve(ld[root]);
}
if(rd[root]!=-)
{
cout<<" "<<rd[root];
solve(rd[root]);
}
else return;
} int main()
{
while(~scanf("%d",&num))
{
memset(ld,-,sizeof(ld));
memset(rd,-,sizeof(rd));
for(i=;i<=num;i++)
{
scanf("%d",&a);
if(i==){root=a;}
else build(root,a);
}
cout<<root;
solve(root);
cout<<endl;
}
return ;
}

HDU 3999 二叉排序树的更多相关文章

  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. 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 ...

  6. hdu 3999 二叉查找树

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

  7. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. HDU 5444 Elven Postman 二叉排序树

    HDU 5444 题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼) 思路:这他妈又是个大水题,可是我还是太傻逼.1000个点的树,居 ...

  9. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

随机推荐

  1. emacs最简单入门,只要10分钟

    macs最简单入门,只要10分钟  windwiny @2013    无聊的时候又看到鼓吹emacs的文章,以前也有几次想尝试,结果都是玩不到10分钟就退出删除了. 这次硬着头皮,打开几篇文章都看完 ...

  2. C# C++联调

    http://jingyan.baidu.com/article/fcb5aff7926344edab4a714d.html

  3. Ecshop商品促销时间精确到小时分钟和秒的设置方法 调用时间

    第一步:找到admin/tempate/good_info.htm文件 把<input name="selbtn1" type="button" id=& ...

  4. Django基础,Day10 - template 模板引擎与路径设置

    作为一个Web框架,Django需要一个方便的方式来生成动态的HTML.最常见的方法依赖于模板.模板包含所需的HTML输出的静态部分以及一些特殊的语法描述如何插入动态内容. Django框架后端默认支 ...

  5. Python之with语句

    Python之with语句 在Python中,我们在打开文件的时候,为了代码的健壮性,通常要考虑一些异常情况,比如: try: ccfile = open('/path/data') content ...

  6. javascript生成二维码

    参考: http://www.w3dev.cn/article/20140617/javascript-create-QR-code.aspx

  7. Orchard源码分析(3):Orchard.WarmupStarter程序集

    概述 Orchard.WarmupStarter程序集包含三个类:WarmupUtility.WarmupHttpModule和Starter<T>.该程序集主要为Orchard应用启动初 ...

  8. 浅谈JavaScript中的能力检测

    引言 我们知道,各个版本的浏览器有着许多不一致性.理想状态下,应该是所有的浏览器都提供一套标准的API接口.但是现实中,各个版本的浏览器存在的怪癖非常多,我们通常都是使用客户端检测来作为补救措施.但是 ...

  9. C++基础入门

    #include "iostream" using namespace std; class A{ public:      A(int x1){       x = x1;  } ...

  10. Chrome插件开发

    参考文档: http://open.chrome.360.cn/extension_dev/overview.html 参考博文: http://www.cnblogs.com/mfryf/p/370 ...