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二叉搜索树寻找众数
这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回 ...
随机推荐
- [Spring MVC] - InitBinder验证
Spring MVC使用InitBinder验证: 使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的,也即比较复杂的验证情况下才会使用.大部份简单的 ...
- 【转】前端精选文摘:BFC 神奇背后的原理
BFC 已经是一个耳听熟闻的词语了,网上有许多关于 BFC 的文章,介绍了如何触发 BFC 以及 BFC 的一些用处(如清浮动,防止 margin 重叠等).虽然我知道如何利用 BFC 解决这些问题, ...
- 关闭和开启oracle
1.使用sqlplus 启动和关闭数据库. 答:使用sqlplus以sysdba的身份登录数据库 因为我的数据库是启动状态,所以我就先演示数据库的关闭 数据库的关闭使用语句shutdown immed ...
- Object类概述
Object:类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.每个类都直接或者间接的继承自Object类. Object类的方法:public int hashCode() ...
- java 线程的优先级
//线程的优先级 //线程1 class xc1 implements Runnable{ public void run(){ for(int i=0;i<20;i++){ System.ou ...
- (gridcontrol等)通用导出excel z
关于DevExpress Winform 的所有可打印控件的导出excel 的通用方法,并且解决DevExpress控件自带的方法存在的缺陷问题 1.解决GridControl自带方法不能导出图片: ...
- 使用AJAX完成用户名是否存在异步校验
一.JSP代码: 1.事件触发:onblur 2.编写AJAX代码:向Action中提交,传递username参数 <script> function checkUsername(){ / ...
- git学习系列--六分之一
版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统,可以对任何类型的文件进行版本控制. 细说分布式版本控制系统 在这类系统中,像 Git,Mercurial,Bazaar 以 ...
- 本机搭建外网web服务器
版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 首先声明一下楼主是个开发人员,按理说这些搭建服务器什么的,和楼主半毛钱的关系都没有.但是呢,楼主是个爱学习的人,懂得德智体全面 ...
- dedecms代码研究三
上次,我们从dedecms的index.PHP文件中了解到了很多信息,也提出了一些问题: 1)加载了/include/common.inc.php,里面做了哪些工作? 2)/include/arc.p ...