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/Others)
Total Submission(s): 835 Accepted Submission(s): 453
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.
1 3 4 2
题意是给你一个序列建立一棵二叉搜索树 要你找出另外一个序列可以建立和原序列建立的二叉搜索树一样且这个序列是字典序最小,一看根据二叉搜索树的特点 左孩子小 右孩子大 直接输出一个先序遍历就OK!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<cstdlib> using namespace std; const int N=; struct Tree{
Tree *l,*r;
int x;
}tree; Tree *root; Tree *Create(Tree *rt,int x){
if(rt==NULL){
rt=(Tree *)malloc(sizeof(Tree));
rt->x=x;
rt->l=rt->r=NULL;
return rt;
}
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
rt->l=Create(rt->l,x);
else //else insert k to the right sub-tree
rt->r=Create(rt->r,x);
return rt;
} void PreOrder(Tree *rt,int x){ //先序历遍
if(x==)
printf("%d",rt->x);
else
printf(" %d",rt->x);
if(rt->l!=NULL)
PreOrder(rt->l,);
if(rt->r!=NULL)
PreOrder(rt->r,);
} int main(){ //freopen("input.txt","r",stdin); int n,x;
while(~scanf("%d",&n)){
root=NULL;
for(int i=;i<n;i++){
scanf("%d",&x);
root=Create(root,x);
}
PreOrder(root,);
printf("\n");
}
return ;
}
HDU 3999 The order of a Tree (先序遍历)的更多相关文章
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- <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 ...
- HDU 3999 The order of a Tree 二叉搜索树 BST
建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...
- 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 ...
- HDU 3999 二叉排序树
The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...
- 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 ...
- 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 ...
- 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/ ...
随机推荐
- Oracle导入excel数据快速方法
Oracle导入excel数据快速方法 使用PLSQL Developer工具,这个可是大名鼎鼎的Oracle DBA最常使用的工具. 在单个文件不大的情况下(少于100000行),并且目的 ...
- linux测试工程介绍(Linux Test Project)
http://ltp.sourceforge.net/ Linux Test Project, 后台很硬,由SGI™ 发起, IBM维护,所以质量有保障. 里面介绍了很多工具,对于一般的基准测试应该是 ...
- Hessian 原理分析
Hessian 原理分析 一.远程通讯协议的基本原理 网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络 IO 来实现,其中传输协议比较出名的有 http . tcp . u ...
- hibernate的入门crud
package com.test; import com.demo.User; import org.hibernate.HibernateException; import org.hibernat ...
- InfluxDB和MySQL的读写对比测试
今天进行了InfluxDB和MySQL的对比测试,这里记录下结果,也方便我以后查阅. 操作系统: CentOS6.5_x64InfluxDB版本 : v1.1.0MySQL版本:v5.1.73CPU ...
- 在openerp撰写消息中增加图片
openerp的撰写消息中, 在文本输入框中, 具有设置文本字体,设置对齐方式 等多种功能, 就像像写这篇新浪blog一样, 可以输入富文本信息. 美中不足的是, 它不能插入图片. 我们如何才能让op ...
- Android 虚拟现实(virtual reality)入门指南
入门指南 本文档介绍怎样使用实验性的 Cardboard SDK for Android 创建您自己的虚拟实境 (VR) 体验. Android 演示版应用:Treasure Hunt 本教程中的代码 ...
- iOS中TableView小技巧
摘要: TableView是ios开发中经经常使用到的控件,这里统一记录一下开发中遇到的经常使用小技巧,不断探索更新.也希望大家能够告诉我很多其它经常使用的小技巧啦~一起进步 1.去除多余的列表线条 ...
- PRM路径规划算法
路径规划作为机器人完成各种任务的基础,一直是研究的热点.研究人员提出了许多规划方法:如人工势场法.单元分解法.随机路标图(PRM)法.快速搜索树(RRT)法等.传统的人工势场.单元分解法需要对空间中的 ...
- django 文件上传 研究
http://python.usyiyi.cn/django/index.html http://python.usyiyi.cn/django/topics/http/file-uploads.ht ...