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. Linux:文件类型和权限

    一个目录要同时具有读权限和执行权限才可以打开,而一个目录要有写权限才允许在其中创建其它文件.

  2. SharePoint API如何处理时区问题

    使用SharePoint API,我们经常会有时区转换的问题,SharePoint API 本身如何处理时区问题的呢? 本文主要以Modified字段为例测试相关API的行为. CSOM API测试: ...

  3. docker解决数据存储问题的方案

    现在docker在云计算领域发展的势头很猛,各个公司不论大小都开始研究这个开源工具和技术,围绕docker的开源项目和创业公司也多如牛毛,就是一个简单管理container的web ui都有很多开源项 ...

  4. iOS-OC内存管理

    目标 1.[理解]内存管理 2.[掌握]第一个MRC程序 3.[掌握]内存管理的原则 4.[理解]野指针与僵尸对象 5.[理解]单个对象的内存管理 6.[理解]多个对象的内存管理 7.[掌握]set方 ...

  5. XML的简单学习

    推荐入门级学习XML网址W3c     http://www.w3school.com.cn/xml/xml_intro.asp 1: XML 指可扩展标记语言    XML 被设计用来传输和存储数据 ...

  6. Factory模式

    使用new的Code都违反了DIP. 但是,依赖于稳定的具体类,是无害的.例如string. 另一方面,对于正在开发中的APP,很多具体类是易变的.此时应该依赖于抽象接口. Factory模式:只依赖 ...

  7. 通过ReentrantLock源代码分析AbstractQueuedSynchronizer独占模式

    1. 重入锁的概念与作用       reentrant 锁意味着什么呢?简单来说,它有一个与获取锁相关的计数器,如果已占有锁的某个线程再次获取锁,那么lock方法中将计数器就加1后就会立刻返回.当释 ...

  8. GO語言基礎教程:Hello world!

    首先簡單地說一下GO語言的環境安裝,從 http://golang.org/dl/ 針對自己的操作系統選擇合適的安裝包,然後下載安裝即可,下載的時候注意別選錯了的操作系統,例如go1.3.1.darw ...

  9. webRTC-实时流媒体的福音

    WebRTC是一项在浏览器内部进行实时视频和音频通信的技术,是谷歌2010年以6820万美元收购Global IP Solutions公司而获得的一项技术.[1] WebRTC实现了基于网页的视频会议 ...

  10. 让input支持 ctrl v上传粘贴图片? 让input支持QQ截图或剪切板中的图像数据(Java实现保存)

    原理:监听粘贴 → 获取粘贴内容 → 将内容上传 → 抓取后返回替换至input 我们在生产中用到的界面: 测试地址 http://sms.reyo.cn 用户名:aa 密码:123456 以下是PH ...