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. Runner站立会议02

    开会时间:21.10~21.30 地点:二教103 今天做了什么:学习五大布局的使用方法 明天准备做什么:学习数据的存储 遇到的困难:知识点太多,信心受挫 站立会议图: 燃尽图:

  2. ansible执行playbook时间显示的python脚本

    import datetime import os import time from ansible.plugins.callback import CallbackBase class Callba ...

  3. 最好用的placeholder插件,jQuery插件EnPlaceholder

    EnPlaceholder插件支持密码框哦!实际对比同类的placeholder插件在ie等浏览器下效果做好! 插件效果预览:http://www.wufangbo.com/demo/jquery/3 ...

  4. Winform添加Label

    Info from : http://www.csharpwin.com/csharpspace/6253r7952.shtml 本例子主要是介绍如何在 C#开发WinForm中加入一个组件,如果你想 ...

  5. nginx basic auth 登陆验证模块

    #1. 新建一个pw.pl文件专门用来生成密码 #!/usr/bin/perl use strict; my $pw=$ARGV[0]; print crypt($pw,$pw)."\n&q ...

  6. chrome调试文章

    http://blog.csdn.net/a6225301/article/details/20207191#t1 http://www.360doc.com/content/13/1220/11/8 ...

  7. 使用 Elasticsearch ik分词实现同义词搜索(转)

    1.首先需要安装好Elasticsearch 和elasticsearch-analysis-ik分词器 2.配置ik同义词 Elasticsearch 自带一个名为 synonym 的同义词 fil ...

  8. jquery判断checkbox是否选中及改变checkbox状态

    转自:http://blog.csdn.net/limingchuan123456789/article/details/11499665 jquery判断checked的三种方法:.attr('ch ...

  9. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  10. 使用MVVM框架avalon.js实现一个简易日历

    最近在做公司内部的运营管理系统,因为与日历密切相关,同时无需触发条件直接显示在页面上,所以针对这样的功能场景,我就用avalon快速实现了一个简易日历,毕竟也是第一次造日历这种轮子,所以这里记录下我当 ...