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. 【NOIP2013模拟】水叮当的舞步

    题目 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变. 为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~ 地毯上的格子有N行N列, ...

  2. Python 列表(List)Ⅱ

    删除列表元素 可以使用 del 语句来删除列表的元素,如下实例: . 以上实例输http://www.xuanhe.net/出结果: 注意:我们会在接下来的章节讨论remove()方法的使用 Pyth ...

  3. 文件操作:rewind()

    函数名: rewind() 功 能: 将文件内部的位置指针重新指向一个流(数据流/文件)的开头   注意:不是文件指针而是文件内部的位置指针,随着对文件的读写文件的位置指针(指向当前读写字节)向后移动 ...

  4. Java的格式化输出

    在java中除了有System.out.println();和System.out.print();之外还有一种格式化的输出,用来限制宽度,保留小数点后的位数,还有对齐方式. 代码: package ...

  5. 深入理解php的输出缓冲区(output buffer)

    这篇文章是翻译自Julien Pauli的博客文章PHP output buffer in deep,Julien是PHP源码的资深开发和维护人员.这篇文章从多个方面讲解了PHP中的输出缓冲区以及怎么 ...

  6. pycharm2019连接mysql错误08801 ------Connection to django1@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.

    Error:Connection to django1@localhost failed. [08001] Could not create connection to database server ...

  7. 任泽平:95页PPT分析2018(经济、房价、政策)

    任泽平:95页PPT分析2018(经济.房价.政策) 2017-12-07 06:38房价

  8. 算法中Amortised time的理解

    ref:http://stackoverflow.com/questions/200384/constant-amortized-time 如果非要翻译成中文,我觉得摊算时间或均摊时间(注意,它和平均 ...

  9. RTMP服务器的延迟,多级边缘不影响延迟,gop为最大因素

    转自:http://blog.chinaunix.net/uid-26000296-id-4932826.html 编码器用FMLE,用手机秒表作为延迟计算. 结论: 1. 影响延迟的三个重要因素:网 ...

  10. iptables添加、删除端口

    简洁才是王道, 下面是添加一个udp端口,端口号8566,即接收到8566端口的所有udp包 /sbin/iptables -I INPUT -p udp --dport -j ACCEPT 要删除这 ...