The order of a Tree

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 64   Accepted Submission(s) : 34

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

41 3 4 2

Sample Output

1 3 2 4
#include <iostream>
#include <stdio.h>
#include <string.h>using namespace std;#define N 100005
int tree[N],l[N],r[N],a[N],num,flag,m;void fx(int k,int x)
{if( x <= tree[k] ){if(l[k] == -1) l[k] = flag;else fx(l[k],x);}else{if(r[k] == -1) r[k] = flag;else fx(r[k],x);}
}void f(int k)
{a[m++] = tree[k];if(l[k] != -1)f(l[k]);if(r[k] != -1)f(r[k]);
}
int main()
{int n,i,x,root;while(~scanf("%d",&n)){memset(l,-1,sizeof(l));memset(r,-1,sizeof(r));root = -1;flag = 0;for(i = 0; i< n; i++){scanf("%d",&x);if(root == -1)tree[++root] = x;else{tree[++flag] = x;fx(root,x);}}m = 0;f(root);for(i = 0; i < m -1; i++)printf("%d ",a[i]);printf("%d\n",a[m-1]);}return 0;
}

HDU3999-The order of a Tree相关推荐

  1. HDU3999 The order of a Tree【二叉搜索树 + 前序遍历】

    The order of a Tree 题意: 给定一个二叉搜索树的插入顺序,要求输出建树之后的前序遍历. 题解: 连续第二天做到二叉搜索树了,真的吹爆这个算法,从二叉树啥也不懂,到简单搜索树直接秒杀 ...

  2. hdu3999 The order of a Tree(BST的建立)

    http://acm.hdu.edu.cn/showproblem.php?pid=3999 题意:给你一个序列可以构成一个二叉搜索树,求此二叉搜索树字典序最小的输入序列. 思路:这题只要明确一点就可 ...

  3. The order of a Tree hdu3999 二叉搜索树

    题目链接: hdu3999 The order of a Tree 题意: 给一个二叉搜索树的序列,求输出可以构成这个二叉搜索树的且满足字典序最小的插入序列 找了一个比较具体描述怎么根据序列建立二叉搜 ...

  4. The order of a Tree(模拟BST的建树和访问)

    题目链接 Problem Description As we know,the shape of a binary search tree is greatly related to the orde ...

  5. leetcode【102,103】Binary Tree Level Order Traversal Binary Tree Zigzag Level Order Traversal

    问题描述(102): Given a binary tree, return the level order traversal of its nodes' values. (ie, from lef ...

  6. hdu 3999The order of a Tree

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3999 本题为简单二叉排序树,先按排序树创建树,然后先序遍历二叉树,输出的时候最后一个数字后面没有空格. ...

  7. HDU 3999 The order of a Tree

    /*二叉树的插入,前序遍历. */#include<iostream> #include<cstring> #include<stdio.h> #include&l ...

  8. 01 二叉树的BFS(广度、层次或水平遍历实现)【Binary Tree 二叉树】

    二叉树的遍历分为BFS和DFS两种大类 下面完整实现BFS遍历二叉树 * 例如二叉树* 1* / \* 2 3* /\* 4 5 BFS遍历结果:1-2-3-4-5 具体的代码实现: 方法一.采用递归 ...

  9. Linux进程浏览器htop安装与使用

    htop 是一个 Linux 下的交互式的进程浏览器,可以用来替换Linux下的top命令.当前具有按树状方式来查看进程,支持颜色主题,可以定制等特性.其实htop是top的加强版,增加了很多功能. ...

最新文章

  1. C语言引用不同路径下的头文件的方法
  2. python json数据的转换
  3. Leaflet中使用awesome-markers插件显示带图标的marker
  4. SharpZipLib使用示例
  5. python matplotlib画折线图出现连线混乱_python使用matplotlib模块绘制多条折线图、散点图...
  6. tiny4412(用户手册及芯片手册)
  7. 如何保证消息队列的高可用?透彻分析源码
  8. 垂直和水平居中方法小结
  9. Dell Latitude 3490 使用 UEFI+GPT 安装 Win7 x64
  10. 第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛(同步赛)BIJ,签到抽奖
  11. Cesium 鼠标单击和双击事件
  12. c语言 统计素数并求和
  13. Flash播放器常用参数设置_应用技巧
  14. 计算机毕业设计 SSM+Vue音乐播放网站系统 云音乐播放系统 付费音乐播放系统Java Vue MySQL数据库 远程调试 代码讲解
  15. 算法系列之算法学习书籍以及资料推荐
  16. 392高校毕业设计选题
  17. 人生在世,最大的笑话莫过于自作多情...
  18. C语言:有一个一维数组score,内放10个学生成绩,求平均成绩。
  19. 地面三维激光扫描仪在火灾现场调查取证中的应用
  20. 偏微分方程的MATLAB解法

热门文章

  1. img.ph.126.net
  2. 锁定计算机游戏怎么就退出,电脑玩游戏老是自动退出了是为什么
  3. 弃用基于帧的视觉感知,奔驰/博世/Mobileye瞄准新方向
  4. 韦布尔分布 matlab,MATLAB绘制威布尔分布曲线
  5. 自定义小程序popupwindow弹出框
  6. python 比较两个json文件
  7. Java通信方式总结
  8. 第一篇:飞镖自动靶的设想与实践
  9. [问题解决]delphi中找不到DCU文件问题解决方案
  10. Web 和 Web标准