The order of a Tree

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

解释:

给定一个序列,构成一颗二叉排序树,然后要求计算出一个新的序列,还是原先序列的数,所构造的二叉排序树也一样,并且字典序还是最小的。

例如:4 6 5 7 2 1 3

那么要求最小的字典序。对于二叉排序树,根节点是最先构造好的,并且对于左孩子和有孩子的添加新元素是不会有影响的,也就是说当一个新的数插入的时候,要么在左孩子中,要么在右孩子中,并且要求一个新的序列并且构造的二叉排序树还是一样的,那就是先给出左边的孩子,再给右边的孩子。或者先给右边的孩子,再给左边的孩子。对于要求字典序最小,就先给左孩子,但是根节点要先给出来,不然二叉排序数就会乱。所以,就是一个先序遍历的过程。这个二叉排序树的答案就是 4 2 1 3 6 5 7. 同时我们再看看后序:1 3 2 5 7 6 4, 树的结构变了,中序:1 2 3 4 5 6 7,输的结构也变了。层次遍历:4 2 6 1 3 5 7,树的结构没有变,但是不如先序便来的字典序小。一开始我就是输出层次遍历的结果,然后发现不对。啦啦啦啦,其实是我一开始就没有看懂这些英语,我以为是换一个不一样的序列,然后二叉排序树一样就可以了。之后才发现least lexicographic。

 #include<bits/stdc++.h>

 using namespace std;

 const int N = ;

 struct point{
int value;
int rx, lx;
} res[N]; int t = , n;
void Out(int i) { if (!res[i].value) return ;
printf("%d%c", res[i].value, t++ == n- ? '\n' : ' ');
Out(res[i].lx);
Out(res[i].rx); } int main () { while (~scanf("%d", &n)) {
memset(res, , sizeof(res));
for (int i = ; i <= n; i++) {
int x, tx = ;
scanf("%d", &x);
if (i == ) {
res[].value = x;
} else {
while (true) {
if (x < res[tx].value) {
if (!res[tx].lx) {
res[tx].lx = i;
res[i].value = x;
break;
} else tx = res[tx].lx;
} else {
if (!res[tx].rx) {
res[tx].rx = i;
res[i].value = x;
break;
} else tx = res[tx].rx;
}
}
}
} // for (int i = 0; i <= n; i++) {
// printf("%d %d %d %d\n", i, res[i].value, res[i].lx, res[i].rx);
// }
Out(); }
return ;
}

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

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

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

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

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

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

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

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

  8. 二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)

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

  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. 伸展树splay之求区间极值

    前言 这篇博客是根据我在打这道题的时候遇到的问题,来打的,有些细节可能考虑不到. 题目 在N(1<=N<=100000)个数A1-An组成的序列上进行M(1<=M<=10000 ...

  2. [洛谷P3322] SDOI2015 排序

    问题描述 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的 i(1<=i<=N),第i中操作为将序列 ...

  3. Java——序列化 反序列化

    记录一下: 先粘两个比较繁琐的方法: put: public void putSerializableObject(String key, Object value, int expireTime) ...

  4. 【leetcode&CN&竞赛】1198.Find Smallest Common Element in All Rows

    题目如下: 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [ ...

  5. 6. ClustrixDB 备份恢复

    ClustrixDB备份恢复:   一.传统MySQL的备份/恢复 shell> mysqldump -u user -h clustrix host --single-transaction ...

  6. CentOS7安装codeblocks

    1.yum -y install epel-release 2.yum clean all && yum makecache 3.yum -y install gtk2-devel c ...

  7. C/C++题库

    1.下面的代码输出什么?为什么? void foo(void) { unsigned int a = 6; int b = -20; (a+b > 6)?puts(“>6”):puts(“ ...

  8. jQuery file upload上传图片出错分析

    以https://github.com/blueimp/jQuery-File-Upload/blob/master/basic-plus.html为例 注释掉load-image.all.min.j ...

  9. 【重点突破】—— UniApp 微信小程序开发官网学习One

    一.初步认识 uni-app官网:https://uniapp.dcloud.io/component/README HBuilderX官方IDE下载地址: http://www.dcloud.io/ ...

  10. leetcode-mid-dynamic programming-55. Jump Game

    mycode  71.47% 思路: 既然要到达终点,那么俺就可以倒推,要想到达n,可以有以下情况 1)到达n-1,然后该位置最少可以走一步 2)到达n-2,然后该位置最少可以走两步 3)到达n-3, ...