这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999

 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
  解题思路:我的标题已经指出了这是水题,套用二叉搜索树的模板就行。另外在输出的时候注意空格即可,如果好几次没有ac的话可以参考下输出。
 #include <iostream>
#include <cstdlib>
using namespace std; int j; struct node {
int val;
node *lch;
node *rch;
}; node *creat (node *p, int x) {
if (p == NULL) {
node *q = new node;
q->val = x;
q->lch = q->rch = NULL;
return q;
}
else {
if (x < p->val) p->lch = creat (p->lch,x);
else p->rch = creat (p->rch,x);
return p;
}
}
void pre_order (node *s) {
//int j = 0;如果写这里--->每一次进来都是0
if (s) {
if (j > )
cout << " ";
j++;
cout << s->val;
pre_order (s->lch);
pre_order (s->rch);
}
} int main() {
int n,x;
while (cin >> n) {
j = ;
node *root = NULL;
for (int i = ;i < n; ++i) {
cin >> x;
root = creat(root,x);
}
pre_order (root);
cout << endl;
}
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

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  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. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  7. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  8. 暑假训练Round1——G: Hkhv的水题之二(字符串的最小表示)

    Problem 1057: Hkhv的水题之二 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  9. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. UIButton根据不同的状态设置layer

    这需要用到KVO,监听button的highlighted属性的变化,在监听回调里根据监听到得属性值设置layer 设置监听如下 [button  addObserver:self forKeyPat ...

  2. 一位IT牛人的十年经验之谈

    1.分享第一条经验:“学历代表过去.能力代表现在.学习力代表未来.” 其实这是一个来自国外教育领域的一个研究结果.相信工作过几年.十几年的朋友对这个道理有些体会吧.但我相信这一点也很重要:“重要的道理 ...

  3. MVC+Front Controller

    MVC+Front Controller 在我前面一篇博文<逃脱Asp.Net MVC框架的枷锁,使用Razor视图引擎>发表之后,很多人关心,脱离了之后怎么办?那么这可以说是它的续篇了. ...

  4. asp.net 上传文件

    文件上传实例 公司产品中一直是采用 flash 实现文件上传功能,但用户的需求多了以后遇到了越来越多难以解决的问题,最后试着用硕正提供的freeform.小型页面控件来解决. 硕正文件上传的实现途径有 ...

  5. img onerror事件

    怪自己知道的太少,img标签有onerror这个事件,我是才刚知道,恕我愚昧,既然是第一次遇到,而且又是一个自己从没有涉及过得的东西,所以我希望通过这个随笔来是自己印象深刻,此文仅仅只是让自己印象深刻 ...

  6. 解析JSON、扩展Fiddler

    解析JSON.扩展Fiddler 按文章结构,这部分应该给出WCFRest项目示例,我想WinForm示例足够详尽了,况且WCFRest还不需要使用插件AppDomain那一套,于是把最近写的Fidd ...

  7. MS Sql Server 消除重复行 保留信息完整的一条 2011-11-26 13:19(QQ空间)

    select company ,count(company) as coun into myls from mylist group by company having count(company)& ...

  8. linux常见笔试题

    一.填空题: 1. 在Linux系统中,以 文件 方式访问设备 . 2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统. 3. Linux文件系统中每个文件用 i节点 来 ...

  9. Android开发(30)--AutoCompleteTextView和MultiAutoCompleteTextView自动提示输入内容

    首先大家都见过类似这种效果, AutoCompleteTextView是实现动态匹配输入的内容 下面就通过一个实例来说明AutoCompleteTextView,同样,AutoCompleteText ...

  10. Android打开系统设置

    今天在做项目过程中,遇到一个问题:用户体验某个功能时需要查看用户是否已经打开了GPS定位服务,若没有则要求进入定位服务设置界面. 下面就直接贴出代码 以下代码是放在了Button的监听事件里,只贴出重 ...