1. /******************************************************************
  2. 题目: The order of a Tree(hdu 3999)
  3. 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3999
  4. 题意: 给你一个序列建立一棵二叉搜索树 要你找出另外一个序
  5. 列,可以建立和原序列建立的二叉搜索树一样且这个序列
  6. 是字典序最小
  7. 算法: 二叉搜索树
  8. 思想: 对于一个二叉搜索树,它的先序遍历是能建立该二叉搜索
  9. 树字典序最小序列
  10. ******************************************************************/
  11. #include<cstdio>
  12. #include<cstring>
  13. #include<cstdlib>
  14. #include<iostream>
  15. using namespace std;
  16.  
  17. typedef struct Tree
  18. {
  19. Tree *left,*right;
  20. int num;
  21. }Tree;
  22. Tree *t;
  23.  
  24. Tree *inser(Tree *p,int x)
  25. {
  26. if (p==NULL)
  27. {
  28. p=(Tree *) malloc(sizeof(Tree));
  29. p->left=p->right=NULL;
  30. p->num=x;
  31. return p;
  32. }
  33. if (p->num>x)
  34. {
  35. p->left=inser(p->left,x);
  36. return p;
  37. }
  38. else
  39. {
  40. p->right=inser(p->right,x);
  41. return p;
  42. }
  43. }
  44.  
  45. void Find(Tree *p,int flag)
  46. {
  47. if (p==NULL) return ;
  48. if (flag) printf("%d",p->num);
  49. else printf(" %d",p->num);
  50. Find(p->left,);
  51. Find(p->right,);
  52. delete(p);
  53. }
  54.  
  55. int main()
  56. {
  57. int n;
  58. while (~scanf("%d",&n))
  59. {
  60. t=NULL;
  61. for (int i=;i<n;i++)
  62. {
  63. int a;
  64. scanf("%d",&a);
  65. t=inser(t,a);
  66. }
  67. Find(t,);
  68. printf("\n");
  69. }
  70. }

hdu 3999 The order of a Tree (二叉搜索树)的更多相关文章

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

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

  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. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  5. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  6. 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...

  7. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. [LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数

    这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回 ...

随机推荐

  1. nodejs:express API之res.locals

    在从零开始nodejs系列文章中,有一个login.html文件 再来看它的get方法,我们并没有看到mess字段.那mess到底是从哪里来的呢? 接着我看到app.js文件里面: 只有这里出现了me ...

  2. android学习笔记51——SQLite 手势Gesture

    手势Gesture 所谓手势,是指用户手指或触摸笔在触摸屏幕上的连续触碰行为. Androi对两种手势行为都提供了支持: 1.对于第一种手势而言,android提供了手势检测,并为手势检测提供了相应的 ...

  3. urlrewriter的使用

    开源类库地址 https://github.com/sethyates/urlrewriter/find/master <?xml version="1.0"?> &l ...

  4. 初学RabbitMQ

    一.RabbitMQ中的一些概念 (1)Connection(连接) 与RabbitMQ建立连接,由ConnectionFactory创建每个Connection至于一个物理server进行连接,此链 ...

  5. KVM虚拟机管理

    #定义新的存储池 virsh pool-define-as spool4lj dir - - - - "/home/lj/spool4lj" virsh pool-build sp ...

  6. 【转】CSS z-index 属性的使用方法和层级树的概念

    文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...

  7. typedef struct 结构体

    typedef struct _TTTT_ {   int    i;  }TT_TT; 定义变量如下: struct _TTTT_  NewTT;方法1 TT_TT NewTT;方法2 是声明和定义 ...

  8. Python类属性,实例属性

    1.Python类数据属性:定义在类里面但在函数外面的变量,它们都是静态的. #一段很简单的代码,但反应了很多 >>> class A(): a=1 #一个类里面有个属性a > ...

  9. RabbitMQ(三)

    官方的使用教程(测试运行) 1."Hello World!" -- 发送接收 We're about to tell the server to deliver us the me ...

  10. WebStorage 和 Cookie的区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...