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. Java面试题系列(六)优化tomcat配置

    序言 资料 如何优化tomcat配置(从内存.并发.缓存3个方面)优化

  2. java文件分片上传,断点续传

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  3. C语言 - C语言简介

    一.C语言的发展史 C语言的发展离不开B语言,我们先从B语言说起.在1970年时,贝尔实验室的肯.汤姆逊将BCPL语言进行修改,由于肯.汤姆逊是从BCPL语言进行修改,因此,此时B语言就诞生了,B的含 ...

  4. Kubernetes Deployment故障排除图解指南

     个人K8s还在学习中,相关博客还没有写,准备学第二遍再开始学,发现这篇文章挺好,先转载一下. 原创: 白明的赞赏账户 下面是一个示意图,可帮助你调试Kubernetes Deployment(你可以 ...

  5. Mysql安装及用简单用法

    Mysql安装和初始化 初见: Mysql软件 - 服务端软件 - 服务端程序 - 解析指令 - 对文件的操作 - 客户端软件 - 客户端程序 - 发送指令(sql语句) - 解析指令 其他类似软件: ...

  6. Android学习笔记之Menu的ShowAsAction属性的设置

    (1)在res--menu目录下的main.xml文件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...

  7. python中类的设计问题(一些高级问题探讨,函数重载,伪私有,工厂模式,类方法等)

    从这里再次体现了python语言强大的灵活性.某些在高级语言中看似不严谨需要尽量避免的地方在python中都是允许的. 比如: (1),异常可以用来处理错误 (2),方法,类也都可以视为对象. (3) ...

  8. python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库 -转载

    转载请注明出处  “结巴”中文分词:做最好的 Python 中文分词组件,分词模块jieba,它是python比较好用的分词模块, 支持中文简体,繁体分词,还支持自定义词库. jieba的分词,提取关 ...

  9. RCU原理分析

    简介 RCU(Read-Copy Update)是数据同步的一种方式,在当前的Linux内核中发挥着重要的作用.RCU主要针对的数据对象是链表,目的是提高遍历读取数据的效率,为了达到目的使用RCU机制 ...

  10. kaptcha Spring 整合

    转自:http://my.oschina.net/CandyDesire/blog/209364 生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的 ...