The order of a Tree

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

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
题目大意:简历一颗二叉排序树,然后先序遍历。
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <stack>
  5. using namespace std;
  6.  
  7. typedef struct node
  8. {
  9. int data;
  10. node *lchild;
  11. node *rchild;
  12. node()
  13. {
  14. lchild = rchild = NULL;
  15. }
  16. }TreeNode;
  17.  
  18. void CreateTree(TreeNode *&pRoot, int data)
  19. {
  20. if (pRoot == NULL)
  21. {
  22. pRoot = new TreeNode;
  23. pRoot->data = data;
  24. }
  25. else
  26. {
  27. if (data > pRoot->data)
  28. {
  29. CreateTree(pRoot->rchild, data);
  30. }
  31. else
  32. {
  33. CreateTree(pRoot->lchild, data);
  34. }
  35. }
  36. }
  37.  
  38. void PreOrder(TreeNode *pRoot)
  39. {
  40. int nCount = ;
  41. if (pRoot == NULL)
  42. {
  43. return;
  44. }
  45. stack<TreeNode*> Stack;
  46. Stack.push(pRoot);
  47. do
  48. {
  49. TreeNode *p = Stack.top();
  50. Stack.pop();
  51. if (nCount == )
  52. {
  53. printf("%d", p->data);
  54. nCount++;
  55. }
  56. else
  57. {
  58. printf(" %d", p->data);
  59. nCount++;
  60. }
  61. if (p->rchild != NULL)
  62. {
  63. Stack.push(p->rchild);
  64. }
  65. if (p->lchild != NULL)
  66. {
  67. Stack.push(p->lchild);
  68. }
  69.  
  70. } while (!Stack.empty());
  71. }
  72.  
  73. int main()
  74. {
  75. int n, num;
  76. scanf("%d", &n);
  77. TreeNode *pRoot = NULL;
  78. for (int i = ; i < n; i++)
  79. {
  80. scanf("%d", &num);
  81. CreateTree(pRoot, num);
  82. }
  83. PreOrder(pRoot);
  84. printf("\n");
  85. return ;
  86. }

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. jenkins2 pipeline插件的10个最佳实践

    jenkins pipeline的10个最佳实践. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd ...

  2. Node.js 爬虫,自动化抓取文章标题和正文

    持续进行中... 目标: 动态User-Agent模拟浏览器 √ 支持Proxy设置,避免被服务器端拒绝 √ 支持多核模式,发挥多核CPU性能 √ 支持核内并发模式 √ 自动解码非英文站点,避免乱码出 ...

  3. SQL——字符串处理函数

    1) ASCII Format:ASCII ( character_expression ) Function:返回表达式最左端字符的ASCII值. eg: select ASCII('abcdef' ...

  4. Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

    一些重要的知识: mybais-spring.jar及其提供的API: SqlSessionFactoryBean: SqlSessionFactory是由SqlSessionFactoryBuild ...

  5. Atitit.提升软件稳定性---基于数据库实现的持久化 循环队列 环形队列

    Atitit.提升软件稳定性---基于数据库实现的持久化  循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...

  6. Socket通信实例(C#)

    SOCKET原理 一.套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息: ...

  7. Struts2入门3 深入学习

    Struts2入门3 深入学习 处理结果和异常 前言: Struts学习的差不多了,还有最后的一点就收官了就是结果处理和异常处理.前面学习Struts主要围绕就是Action以及struts.xml配 ...

  8. Android中的IOC框架,完全注解方式就可以进行UI绑定和事件绑定

    转载请注明出处:http://blog.csdn.net/blog_wang/article/details/38468547 相信很多使用过Afinal和Xutils的朋友会发现框架中自带View控 ...

  9. SourceTree - 正在检查源... When cloning a repository, "Checking Source" spins forever

    I am trying to clone a repository, my OpenSSH is set up correctly and I can do everything fine in Gi ...

  10. jinkins在windows上的安装 配置C#编译

    首先jinkins在windows上的安装就不说,安装只需要下载相应安装包就可以了,后有些时候经常需要修改端口号.修改如下: 然后重启jenkins服务 首次运行界面 个人建议插件按需安装. 建立一个 ...