hdu 3999 The order of a Tree (二叉搜索树)
- /******************************************************************
- 题目: The order of a Tree(hdu 3999)
- 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3999
- 题意: 给你一个序列建立一棵二叉搜索树 要你找出另外一个序
- 列,可以建立和原序列建立的二叉搜索树一样且这个序列
- 是字典序最小
- 算法: 二叉搜索树
- 思想: 对于一个二叉搜索树,它的先序遍历是能建立该二叉搜索
- 树字典序最小序列
- ******************************************************************/
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<iostream>
- using namespace std;
- typedef struct Tree
- {
- Tree *left,*right;
- int num;
- }Tree;
- Tree *t;
- Tree *inser(Tree *p,int x)
- {
- if (p==NULL)
- {
- p=(Tree *) malloc(sizeof(Tree));
- p->left=p->right=NULL;
- p->num=x;
- return p;
- }
- if (p->num>x)
- {
- p->left=inser(p->left,x);
- return p;
- }
- else
- {
- p->right=inser(p->right,x);
- return p;
- }
- }
- void Find(Tree *p,int flag)
- {
- if (p==NULL) return ;
- if (flag) printf("%d",p->num);
- else printf(" %d",p->num);
- Find(p->left,);
- Find(p->right,);
- delete(p);
- }
- int main()
- {
- int n;
- while (~scanf("%d",&n))
- {
- t=NULL;
- for (int i=;i<n;i++)
- {
- int a;
- scanf("%d",&a);
- t=inser(t,a);
- }
- Find(t,);
- printf("\n");
- }
- }
hdu 3999 The order of a Tree (二叉搜索树)的更多相关文章
- HDU 3999 The order of a Tree 二叉搜索树 BST
建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- 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 ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- [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 ...
- 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...
- [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 ...
- [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 ...
- [LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数
这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回 ...
随机推荐
- nodejs:express API之res.locals
在从零开始nodejs系列文章中,有一个login.html文件 再来看它的get方法,我们并没有看到mess字段.那mess到底是从哪里来的呢? 接着我看到app.js文件里面: 只有这里出现了me ...
- android学习笔记51——SQLite 手势Gesture
手势Gesture 所谓手势,是指用户手指或触摸笔在触摸屏幕上的连续触碰行为. Androi对两种手势行为都提供了支持: 1.对于第一种手势而言,android提供了手势检测,并为手势检测提供了相应的 ...
- urlrewriter的使用
开源类库地址 https://github.com/sethyates/urlrewriter/find/master <?xml version="1.0"?> &l ...
- 初学RabbitMQ
一.RabbitMQ中的一些概念 (1)Connection(连接) 与RabbitMQ建立连接,由ConnectionFactory创建每个Connection至于一个物理server进行连接,此链 ...
- KVM虚拟机管理
#定义新的存储池 virsh pool-define-as spool4lj dir - - - - "/home/lj/spool4lj" virsh pool-build sp ...
- 【转】CSS z-index 属性的使用方法和层级树的概念
文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...
- typedef struct 结构体
typedef struct _TTTT_ { int i; }TT_TT; 定义变量如下: struct _TTTT_ NewTT;方法1 TT_TT NewTT;方法2 是声明和定义 ...
- Python类属性,实例属性
1.Python类数据属性:定义在类里面但在函数外面的变量,它们都是静态的. #一段很简单的代码,但反应了很多 >>> class A(): a=1 #一个类里面有个属性a > ...
- RabbitMQ(三)
官方的使用教程(测试运行) 1."Hello World!" -- 发送接收 We're about to tell the server to deliver us the me ...
- WebStorage 和 Cookie的区别
sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...