The order of a Tree
The order of a Tree
Problem Description
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
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
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的更多相关文章
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- 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 ...
- 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 ...
- 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 ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- 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 ...
- 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 ...
- 二叉排序树: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) ...
- 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 ...
随机推荐
- 【shell】文本按行逆序
1.最简单的方法是使用tac [root ~]$ seq |tac 2.使用tr和awk. tr把换行符替换成自定义的分隔符,awk分解替换后的字符串,并逆序输出 [root ~]$ seq | tr ...
- nmon监控使用
1.上传nmon_linux_x86_64文件到服务器 2.修改文件权限chmod 775 nmon_linux_x86_64 3.压测时需要执行以下命令监控服务器./nmon_linux_x86_6 ...
- opencv加椒盐噪声
void salt(IplImage *img, int saltNum) { int x,y; int i ; unsigned char *src = NULL; src = (unsigned ...
- JPA学习(四、JPA_映射关联关系)
框架学习之JPA(四) JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 ...
- HDU2294--Pendant(DP,矩阵优化)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- yield return的使用。。。
因为要取两个集合不同的元素,所以写了个拓展方法,用到了yield这个关键字,然后就学习了一波.先上代码 public static IEnumerable<T> NoRetainAll&l ...
- PixelShuffle
- Spring的Ioc理解
1.Ioc=控制反转和依赖注入(DI),两个是一回事 控制反转的好处: 把对象的创建和依赖定义在xml中,改变子类的实现变得很简单 控制反转减轻了对象之间的耦合度,减轻了对象之间的依赖关系,增加了系统 ...
- mysql高水位问题解决办法
数据库中有些表使用delete删除了一些行后,发现空间并未释放产生原因:类比Oracle的高水位线产生原理 delete 不会释放文件高水位 truncate会释放 ,实际是把.ibd文件删掉了,再建 ...
- HTTP头详解:
GET/mycode/2.gifHTTP/1.1 [表示发送的是GET请求,请求资源是/mycode/2.gif,协议HTTP/1.1] Host:localhost [主机] Connectio ...