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. 项目协作管理平台-teambition和tapd--深度体验

    ​ 一.分析目的 通过分析2B产品中的团队协作管理软件的对比分析,用于为公司团队协作软件的选型做产考. 二.竞品归属市场概况 2.1.目标用户群及需求 主要面向企业用户,用于解决企业不同地域以及不同职 ...

  2. ios 绘制虚线 CGContextSetLineDash的使用

    画虚线需要用到函数: CGContextSetLineDash 此函数需要四个参数: context – 这个不用多说 phase - 稍后再说 lengths – 指明虚线是如何交替绘制,具体看例子 ...

  3. sql常用操作(三)多表查询

    1 连接查询 1.1连接就是指两个或2个以上的表(数据源)“连接起来成为一个数据源”. 实际上,两个表的完全的连接是这样的一个过程: 左边的表的每一行,跟右边的表的每一行,两两互相“横向对接”后所得到 ...

  4. 如何提高Mysql的查询效率???

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  5. 检查windows端口被占用

    开始---->运行---->cmd,或者是window+R组合键,调出命令窗口 输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是49157,首先 ...

  6. Cookie 没你不行

    Cookie 没你不行 Cookie 没你不行 前言: Cookie 是什么 起源 到底是什么? 使用场景 如何使用cookie Cookie 和http协议 (服务端操作cookie) Cookie ...

  7. event loop、进程和线程、任务队列

    本文原链接:https://cloud.tencent.com/developer/article/1106531 https://cloud.tencent.com/developer/articl ...

  8. VA助手添加扩展文件后缀名

    Allow C/C++ files with a non-standard extension Follow these steps to make Visual Assist consider as ...

  9. 总结一下自己脑海里的JavaScript吧(一)--DOM模型

    今天是2019年6月25日,闲来无事,写一篇文章来看看自己脑袋里装了多少JavaScript知识! 这儿就第一章: 说起JavaScript,它是什么?后端脚本语言?前端编程语言?还是在网站浏览器上运 ...

  10. CVE-2014-1767

    [0x00].简介  CVE-2014-1767漏洞是由于Windows的afd.sys驱动在对系统内存的管理操作中,存在着悬垂指针的问题.在特定情况下攻击者可以通过该悬垂指针造成内存的double ...