二叉排序树:HUD3999-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): 2070 Accepted Submission(s): 1097
Problem Description
As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely:
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.
Input
There are multiple test cases in an input file. The first line of each testcase is an integer n(n <= 100,000),represent the number of nodes.The second line has n intergers,k1 to kn,represent the order of a tree.To make if more simple, k1 to kn is a sequence of 1 to n.
Output
One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.
Sample Input
4
1 3 4 2
Sample Output
1 3 2 4
解题心得:
- 题意就是给你一串数,先生成一个二叉排序树,然后按照字典序输出就可以了。没有什么难点,只是在按照字典序输出的时候注意递归输出方式就可以了。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
int n,index;
struct node
{
int num;
node *lchild,*rchild;
} tree[maxn];
node* newnode(int num)
{
tree[index].num = num;
tree[index].lchild = NULL;
tree[index].rchild = NULL;
return &tree[index++];
}
void insertBST(node*& root,int num)
{
if(root == NULL)
{
root = newnode(num);
return ;
}
else if(root->num > num)
insertBST(root->lchild,num);
else
insertBST(root->rchild,num);
}
void buildtree(node*& root)
{
root = NULL;
int num;
for(int i=1; i<=n; i++)
{
scanf("%d",&num);
insertBST(root,num);
}
}
void print(node*& root,int num)
{
if(root == NULL)
return ;
if(num == 2)
printf(" ");
printf("%d",root->num);//搜到一个输入一个
print(root->lchild,2);//先向左方开始找
print(root->rchild,2);
}
int main()
{
while(scanf("%d",&n) != EOF)
{
node *p;
index = 0;
buildtree(p);//先构建二叉树
print(p,1);//然后输出
printf("\n");
}
}
二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)的更多相关文章
- 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 ...
- The order of a Tree
The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- 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 - 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 ...
- 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 ...
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
随机推荐
- openstack安装newton版本Nova部署(三)
一.控制节点安装部署Nova Nova 包含API(负责接收相应外部请求,支持OpenStackAPI,EC2API):cert:负责身份认证:schedule:用于云主机调度(虚拟机创建在哪台主机上 ...
- c#基础值类和引用类型
//值类型:int double char decimal bool enum struct //引用类型:string 数组 自定义类 集合 object 接口 值传递传递的值得本身 引用传递传递的 ...
- B/S架构 C/S架构 SOA架构
一.什么是C/S和B/S 第一.什么是C/S结构.C/S (Client/Server)结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配 ...
- ios 根据字典自动生成属性
- (void)createPropertyCode{ NSMutableString *codes = [NSMutableString string]; // 遍历字典 [self enumera ...
- mysqldatadir 转移
当mysql data路径与原始目录不一致时 ,请在mysql 安装目录下my-default.ini 进行设置,取消对应#注释的地址,设置新地址,保存,重新启动,即可. 从网上各种搜索啊,各种尝试, ...
- Beginning Python Chapter 3 Notes
变量(variable)是储存数据的实体,在Python中也被称为"名称"(name). 1.Python"名称"基本命名法则 1.1) "名称&qu ...
- Protocol Buffer学习教程之开篇概述(一)
1. Protocol Buffer是什么 Protocol Buffer是google旗下的产品,用于序列化与反序列化数据结构,但是比xml更小.更快.更简单,而且能跨语言.跨平台.你可以把你的数据 ...
- Python之时间表示
Python的time模块中提供了丰富的关于时间操作方法,可以利用这些方法来完成这个需求. time.time() :获取当前时间戳 time.ctime(): 当前时间的字符串形式 time.loc ...
- WAMP安装提示缺少 msvcr100.dll文件解决方法
WAMP安装提示缺少wamp msvcr100.dll文件解决方法 因为wamp基于vs c++2010开发,需要提前安装这个组件才可以正常运行 微软官方组件下载地址: 32位:http://www. ...
- jsp另外五大内置对象之out输出
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...