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 ...
随机推荐
- vue项目刷新页面,使数据不丢失(sessionStorage、localStorage、cookie)
vue项目刷新页面时,存储在vuex中的数据会丢失,把他们存到stroage中可以保证不丢失.
- print和赋值
赋值 #可同时(并行)给多个变量赋值 x, y, z = 1, 2, 3 #交换多个变量的值 x, y = y, x 序列解包(或可迭代对象解包):将一个序列(或任何可迭代对象)解包,并将得到的值存储 ...
- 【java工具类】下载文件
FileUtil.java /** * 下载文件 * @param file; * @param response */ public static void downloadFile(File fi ...
- D5000系统使用手册--AVC
有关概念: AVC:自动电压控制 PAS:网络分析应用 SCADA:电网稳态监控应用 闭环:AVC由SCADA系统获得电网的实时运行状态,分析计算后发出控制指令,电网运行状态变化后反馈回AVC,形成闭 ...
- js基本数据类型+判断数据类型方法
摘要:不管是什么类型的,Object.prototype.toString.call();都可以判断出其具体的类型,简单基本类型(String.Number.Boolean.Null.Undefine ...
- php GD库简单使用和封装
GD库创建图像步骤 <?php //1.创建画布 $width = 300; $height= 200; $image=imagecreatetruecolor($width,$height); ...
- 实现Callable接口,并与Future结合使用
实现步骤: 创建 Callable 接口的实现类,并实现 call() 方法,该 call() 方法将作为线程执行体,并且有返回值. 创建 Callable 实现类的实例,使用 FutureTask ...
- 用JQuery获取事件源怎么写
$(".btn").click(function(e){ // e 就是事件对象 e.target; // 事件的目标 dom e.currentTarget; // 事件处理程序 ...
- Spring 缓存注解 SpEL 表达式解析
缓存注解上 key.condition.unless 等 SpEL 表达式的解析 SpEl 支持的计算变量: 1)#ai.#pi.#命名参数[i 表示参数下标,从 0 开始] 2)#result:Ca ...
- 对vi/vim的一些看法
早在网上看到别人如何把vi吹得神乎其神了,决定试着用一下.不过还是发现太麻烦了,因为我是在windows系统下面的,网络的原因无法使用linux系统(无校园电信客户端). 首先vim只是一个文本编辑器 ...