The order of a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 835    Accepted Submission(s): 453

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
 
Source
 
Recommend
lcy
 

题意是给你一个序列建立一棵二叉搜索树 要你找出另外一个序列可以建立和原序列建立的二叉搜索树一样且这个序列是字典序最小,一看根据二叉搜索树的特点 左孩子小 右孩子大 直接输出一个先序遍历就OK!

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<stack>
  5. #include<cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. const int N=;
  10.  
  11. struct Tree{
  12. Tree *l,*r;
  13. int x;
  14. }tree;
  15.  
  16. Tree *root;
  17.  
  18. Tree *Create(Tree *rt,int x){
  19. if(rt==NULL){
  20. rt=(Tree *)malloc(sizeof(Tree));
  21. rt->x=x;
  22. rt->l=rt->r=NULL;
  23. return rt;
  24. }
  25. if(rt->x>x) //insert a key k to a nonempty tree, if k is less than the root ,insert it to the left sub-tree
  26. rt->l=Create(rt->l,x);
  27. else //else insert k to the right sub-tree
  28. rt->r=Create(rt->r,x);
  29. return rt;
  30. }
  31.  
  32. void PreOrder(Tree *rt,int x){ //先序历遍
  33. if(x==)
  34. printf("%d",rt->x);
  35. else
  36. printf(" %d",rt->x);
  37. if(rt->l!=NULL)
  38. PreOrder(rt->l,);
  39. if(rt->r!=NULL)
  40. PreOrder(rt->r,);
  41. }
  42.  
  43. int main(){
  44.  
  45. //freopen("input.txt","r",stdin);
  46.  
  47. int n,x;
  48. while(~scanf("%d",&n)){
  49. root=NULL;
  50. for(int i=;i<n;i++){
  51. scanf("%d",&x);
  52. root=Create(root,x);
  53. }
  54. PreOrder(root,);
  55. printf("\n");
  56. }
  57. return ;
  58. }

HDU 3999 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 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

  3. 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 ...

  4. HDU 3999 The order of a Tree 二叉搜索树 BST

    建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...

  5. 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 ...

  6. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...

  7. 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 ...

  8. 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 ...

  9. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

随机推荐

  1. linux kernel系列四:嵌入式系统中的文件系统以及MTD

    本节介绍File System和MTD技术 一 FS 熟知的FS有ext2,3,4.但是这些都是针对磁盘设备的.而ES中一般的存储设备为Flash,由于Flash的特殊性: Flash存储按照Bloc ...

  2. Simple XOR Encryption/Decryption in C++ (And Several Other Languages)

    For details on how to implement XOR encryption using Go, see this post. If you are looking for XOR e ...

  3. elasticsearch控制台中文乱码和jvm内存大小调整。 解决办法:

    修改config下面的jvm.options如下 -Xms256m -Xmx256m # ensure UTF-8 encoding by default (e.g. filenames) #-Dfi ...

  4. 关于ThinkPHP的一些编程技巧

    在TP学习过程中难免会遇到一些大大小小的问题,把这些问题积累下来就可以在以后遇到时能很快速的解决,提高编程效率. 1.让Runtime下的文件格式化:入口文件处:define(‘STRIP_RUNTI ...

  5. Google想出了一个决定人员晋升的算法,然后就没有然后了......

    Google 有点跑偏了,逗死我了~实践一下也好~ Prasad Setty 是 Google People Analytics 团队的副总裁.7 年前 Google 成立的这支团队的职责是收集和利用 ...

  6. 【转】application.properties 常见配置

    Various properties can be specified inside your application.properties/application.yml file or as co ...

  7. iOS SDK 从配置文件里读SDK。转化成class 可同时加载多个SDK

    首先在工程中加入XXX  plist 配置文件. 然后在key 输入名字比如allsdk  value 里填写.a 文件的名字 NSString *plistPath = [[NSBundle mai ...

  8. 牛客网-《剑指offer》-包含min函数的栈

    题目:http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49 辅助栈 C++ class Solution { public ...

  9. 开发JAVA9以上的项目时,出现ClassNotFoundException: javax.xml.bind.JAXBException的解决方法

    一.问题描述: 开发JAVA9以上的项目时,出现ClassNotFoundException: javax.xml.bind.JAXBException的解决方法 二.问题样例 三.解决方案 打开mv ...

  10. Windows平台查看端口占用的程序

    一.方法:管理员权限打开Cmd窗口:netstat -obna