The order of a Tree

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2070 Accepted Submission(s): 1097

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


解题心得:

  1. 题意就是给你一串数,先生成一个二叉排序树,然后按照字典序输出就可以了。没有什么难点,只是在按照字典序输出的时候注意递归输出方式就可以了。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
int n,index;
struct node
{
int num;
node *lchild,*rchild;
} tree[maxn]; node* newnode(int num)
{
tree[index].num = num;
tree[index].lchild = NULL;
tree[index].rchild = NULL;
return &tree[index++];
} void insertBST(node*& root,int num)
{
if(root == NULL)
{
root = newnode(num);
return ;
}
else if(root->num > num)
insertBST(root->lchild,num);
else
insertBST(root->rchild,num);
} void buildtree(node*& root)
{
root = NULL;
int num;
for(int i=1; i<=n; i++)
{
scanf("%d",&num);
insertBST(root,num);
}
} void print(node*& root,int num)
{
if(root == NULL)
return ;
if(num == 2)
printf(" ");
printf("%d",root->num);//搜到一个输入一个
print(root->lchild,2);//先向左方开始找
print(root->rchild,2);
} int main()
{
while(scanf("%d",&n) != EOF)
{
node *p;
index = 0;
buildtree(p);//先构建二叉树
print(p,1);//然后输出
printf("\n");
}
}

二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)的更多相关文章

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

  2. The order of a Tree

    The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...

  3. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

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

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

  6. <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

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

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

  9. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

随机推荐

  1. 073 Set Matrix Zeroes 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...

  2. Aspose.word组件介绍

    阅读目录 1.基本介绍 2.文档对象模型概述        本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html 本博客其他.NET开 ...

  3. echarts Hello world 入门

    <!DOCTYPE html> <html> <head> <title></title> <script type="te ...

  4. Ionic开发-搭建开发环境

    1安装node.js 2安装ionic & cordova: 命令行输入:npm install –g cordova ionic 注:-g表示全局安装,也可以进入指定的目录安装,但这里推荐全 ...

  5. sql 2008 中不能创建数据库关系图

    执行以下命令: ALTER AUTHORIZATION ON DATABASE::[databasename] TO sa [databasename] 为数据库名: 此方法借鉴于<老高> ...

  6. MUI获取文本框的值

    MUI事件绑定注意父节点.子节点(也可以是标签选择器) js部分 html部分

  7. 从SAP客户主数据里直接创建商机(Opportunity)

    在SAP CRM Fiori的Account应用里,直接在Account页面创建商机: 在SAP Cloud for Customer里: 要获取更多Jerry的原创文章,请关注公众号"汪子 ...

  8. ansible-galera集群部署

    一.环境准备 1.各主机配置静态域名解析: [root@node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain local ...

  9. 使用ServiceController组件控制计算机服务

    实现效果: 知识运用: ServiceController组件的MachineName属性 //获取或设置服务所驻留的计算机名称 public string MachineName{get;set;} ...

  10. python_104_面向对象总结

    参考(都要认真看看):http://www.cnblogs.com/alex3714/articles/5188179.html http://www.cnblogs.com/alex3714/art ...