The order of a Tree

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

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 <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
using namespace std; typedef struct node
{
int data;
node *lchild;
node *rchild;
node()
{
lchild = rchild = NULL;
}
}TreeNode; void CreateTree(TreeNode *&pRoot, int data)
{
if (pRoot == NULL)
{
pRoot = new TreeNode;
pRoot->data = data;
}
else
{
if (data > pRoot->data)
{
CreateTree(pRoot->rchild, data);
}
else
{
CreateTree(pRoot->lchild, data);
}
}
} void PreOrder(TreeNode *pRoot)
{
int nCount = ;
if (pRoot == NULL)
{
return;
}
stack<TreeNode*> Stack;
Stack.push(pRoot);
do
{
TreeNode *p = Stack.top();
Stack.pop();
if (nCount == )
{
printf("%d", p->data);
nCount++;
}
else
{
printf(" %d", p->data);
nCount++;
}
if (p->rchild != NULL)
{
Stack.push(p->rchild);
}
if (p->lchild != NULL)
{
Stack.push(p->lchild);
} } while (!Stack.empty());
} int main()
{
int n, num;
scanf("%d", &n);
TreeNode *pRoot = NULL;
for (int i = ; i < n; i++)
{
scanf("%d", &num);
CreateTree(pRoot, num);
}
PreOrder(pRoot);
printf("\n");
return ;
}

HDU 3999 The order of a Tree的更多相关文章

  1. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

  2. <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

  3. 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 ...

  4. HDU 3999 The order of a Tree 二叉搜索树 BST

    建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...

  5. 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 ...

  6. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...

  7. 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 ...

  8. 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 ...

  9. 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/ ...

随机推荐

  1. 转载:SQL Server 2008-建立分区表(Table Partition) 转载

    数据库结构和索引的是否合理在很大程度上影响了数据库的性能,但是随着数据库信息负载的增大,对数据库的性能也发生了很大的影响.可能我们的数据库在一开始有着很高的性能,但是随着数据存储量的急速增长—例如订单 ...

  2. [MFC] 从文件读取与向文件添加数据

    CString str,str2,str3;str2="dsf",str3="dsfds"; CStdioFile myFile, File; if(myFil ...

  3. PHP爬虫技术(一)

    摘要:本篇文章介绍PHP抓取网页内容技术,利用PHP cURL扩展获取网页内容,还可以抓取网页头部,设置cookie,处理302跳转. 一.cURL安装 采用源码安装PHP时,需要在configure ...

  4. Linux:Linux 重要人物

    1.Ken Thompson:C 语言之父和 UNIX 之父 2.Dennis Ritchie:C 语言之父和 UNIX 之父 3.Stallman:著名黑客,GNU 创始人,开发了 Emacs.gc ...

  5. JavaScript toFixed function Not Rouding

    JavaScript库函数toFixed用来将给定的数字四舍五入为指定的小数位数,W3school上有详细的介绍.众所周知,在处理小数位四舍五入的时候存在两种方式:一种是逢五进一,如5.885保留两位 ...

  6. R 中同步进行的多组比较的包:npmc

    方差检验可以评估组间的差异.依据检验的结果,虽然你可以拒绝不存在差异的原假设,但方差检验并没有告诉你哪些组显著地与其他组有不同.Robert 在 <R in Action>一书中推荐了一个 ...

  7. 浅谈压缩感知(二十七):压缩感知重构算法之稀疏度自适应匹配追踪(SAMP)

    主要内容: SAMP的算法流程 SAMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.SAMP的算法流程 前面所述大部分OMP及其前改算法都需要已知信号的稀疏度 ...

  8. Cacti学习笔记一:基本安装和配置

    1.安装依赖包 yum -y install net-snmp-devel mysql mysql-devel openssl-devel libtool 2.安装RRDTool yum -y ins ...

  9. 分析一个C语言程序生成的汇编代码-《Linux内核分析》Week1作业

    署名信息 郭春阳 原创作品转载请注明出处 :<Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029000 C源码 这 ...

  10. Leetcode 27 Remove Element STL

    和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...