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/ ...
随机推荐
- 浅谈 Adaboost 算法
http://blog.csdn.net/haidao2009/article/details/7514787 菜鸟最近开始学习machine learning.发现adaboost 挺有趣,就把自己 ...
- Intellij idea断点 Debugger slow: Method breakpoints my dramatically slow down debugging
不知道点到哪里了,IDEA调试特别卡,而且总是如下提示, Debugger slow: Method breakpoints my dramatically slow down debugging 意 ...
- javascript捕获事件event
var e = e ? e : window.event; window.event ? window.event.cancelBubble = true : e.stopPropagation(); ...
- 【转】Andorid中Intent的使用-返回数据给上一个活动
第一个Activity A启动另外一个Activity B,B返回数据给A ============================================================= ...
- HDS Truecopy实现原理及项目的选择-诸多案例
copy from:http://www.eygle.com/archives/2009/05/hds_truecopy_dataguard.html 诸多案例:http://wenku.baidu. ...
- JAVA中JDBC连接Mysql数据库简单测试
一.引用库 maven库:mysql:mysql-connector-java:6.0.6 二.SDK环境 JAVA JDK10 三.测试代码 package com.mysql.mysqlconne ...
- Windows操作系统下的MySQL主从复制及读写分离[转]
mysql主从复制配置 保证主库和从库数据库数据一致 mysql主库MASTER配置(在my.cnf中加入以下配置):log-bin=master-binbinlog-do-db=test #需要 ...
- 火狐浏览器flash经常奔溃的
火狐浏览器flash经常奔溃的 1.首先,在火狐浏览器地址栏在输入:about:config?filter=dom.ipc.plugins.flash.disable-protected-mode,按 ...
- map集合中value()、keySet()、entrySet()区别
在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键, ...
- MySQL事物系列:1:事物简介
1:事物是一组SQL的集合,要么都执行,要么都不执行.有ACID4个特性,即:原子性.一致性.隔离性.持久性. A(Atomicity)原子性:整个事物是不可分割的工作单位. C(consistenc ...