http://acm.hdu.edu.cn/showproblem.php?pid=3999

The order of a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1361    Accepted Submission(s): 695

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 <fstream>
#include <iostream> using namespace std; typedef struct Node{
Node *lch,*rch,*nex;
int x;
Node(int x){
this->x=x;
lch=NULL;
rch=NULL;
}
}inode; int n,tn;
inode *head; void insert(int t);
void preOrder(inode *p); int main()
{
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int t;
while(~scanf("%d",&n)){
scanf("%d",&t);
head=new inode(t);
for(int i=;i<n;i++){
scanf("%d",&t);
insert(t);
}
tn=;
preOrder(head);
}
return ;
}
void insert(int t){
inode *p=head,*s=new inode(t);
while(p!=NULL){
if(t<p->x)
if(p->lch!=NULL) p=p->lch;
else{
p->lch=s;
break;
}
else
if(p->rch!=NULL) p=p->rch;
else{
p->rch=s;
break;
}
}
}
void preOrder(inode *p){
if(p!=NULL){
printf("%d",p->x);
if(++tn<n) printf(" ");
else printf("\n");
preOrder(p->lch);
preOrder(p->rch);
delete p;
}
}

hdu3999-The order of a Tree (二叉树的先序遍历)的更多相关文章

  1. hdu3999 The order of a Tree

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 题意:给一序列,按该序列插入二叉树,给出字典序最小的插入方法建相同的一棵树出来.即求二叉树的先序 ...

  2. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  3. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  4. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  5. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  6. LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal

    题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...

  7. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  8. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  9. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  10. LintCode-67.二叉树的中序遍历

    二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...

随机推荐

  1. orientdb 图数据库docker 安装试用

    1. 镜像 docker pull orientdb 2. 启动 docker run -d --name orientdb -p 2424:2424 -p 2480:2480 -e ORIENTDB ...

  2. torcs代码

    /** Info returned by driver during the race */ typedef struct { tdble steer; /**< Steer command [ ...

  3. BZOJ4974:[lydsy1708月赛]字符串大师

    浅谈\(KMP\):https://www.cnblogs.com/AKMer/p/10438148.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  4. Java格式化时间为String类型

    SimpleDateFormat ormater = new SimpleDateFormat("yyyy-MM-dd"); Date date=new Date(); Strin ...

  5. Bootstrap组件系列之福利篇几款好用的组件(推荐)

    引用 :http://www.jb51.net/article/87189.htm 一.时间组件 bootstrap风格的时间组件非常多,你可以在github上面随便搜索“datepicker”关键字 ...

  6. java代码------计算器

    总结:我用if()语句写计算功能的代码时,实现不了,与switch_-catch语句不一样.不知到怎么实现 package com.p; import javax.swing.*; import ja ...

  7. NGUI的UICamera

    参考 https://blog.csdn.net/kakashi8841/article/details/20548429   全文请查看:http://note.youdao.com/notesha ...

  8. 007:MySQL SSL

    一. SSL安装 SSL(Secure Socket Layer)是维护Client - Server之间加密通讯的一套安全协议: --默认ssl未开启 mysql> show variable ...

  9. cache的作用

    cache的作用就是第一次请求完毕之后,如果再次去请求,可以直接从缓存里面读取而不是再到服务器端读取. 如果使用jquery,可以使用 cache参数来控制 $.ajax({  url: " ...

  10. 使用wifi网卡笔记1----网卡选型、开发环境搭建、内核配置

    1.wifi的STA模式和AP模式 Ap(Access Point)模式指的是可以将网卡设置为路由器用来共享流量或有线网络给别人使用, sta模式指的是当做网卡连接路由器上网 (1):AP也就是无线接 ...