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/ ...
随机推荐
- 用php实现交互式工具——How do I write a command-line interactive PHP script?
I want to write a PHP script that I can use from the command line. I want it to prompt and accept in ...
- JPA(三):JPA基本注解
基本注解 @Entity 标注用于实体类声明语句之前,指出该Java类为实体类,将映射到指定的数据库表.如声明一个实体类Customer,将它映射到数据的coustomer表上. package co ...
- eclipse help说明链接
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Freference%2Fcdt_u_prop_bu ...
- Pandas DataFrame学习笔记
对一个DF r1 r2 r3 c1 c2 c3 选行: df['r1'] df['r2':'r2'] #包含r2 df[df['c1']>5] #按条件选 选列: df['c1'] ...
- (转)Debug Assertion Failed! Expression: _pFirstBlock == pHead
最近在VS上开发C++程序时遇到了这个错误: Debug Assertion Failed! Expression:_pFirstBlock == pHead 如图: 点击Abort之后,查看调用 ...
- OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so
OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so ...
- [Canvas]计时表/秒表
欲观看效果请点击下载,然后用浏览器打开index.html查看. 本作 Github地址:https://github.com/horn19782016/StopWatch 图例: 代码: <! ...
- 解决Jquery向页面append新元素之后事件的绑定问题
/*经过用户技能标签增加样式*/ $(".s-edited").live("hover",function(){ $(this).toggleClass(&qu ...
- 1064: 不明飞行物(ufo)
#include<iostream> #include<string> using namespace std; int main(){ string a,b ; long m ...
- mysql版本导致的hibernate 方言问题
今天在看hibernate视频时,看到视频上人家的hibernate.cfg.xml配置文件在配置hibernate方言时,发现视频上是这样写的<property name="dial ...