这里是杭电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. TPL相关

    C#中的Timer System.Windows.Forms.TimerSystem.Threading.Timer System.Timers.Timer 1.System.Windows.Form ...

  2. data格式加载图片

    html img 使用data格式加载图片   背景 这久闲来无事给一位客户测试一款体检软件,是winform结构的,其中有一个功能是需要把生成的体检报告导出为html格式.测试导出后直接在谷歌浏览器 ...

  3. jQuery获取动态生成的元素

    需求描述:页面上可以动态添加数据,比如table,点击按钮可以动态添加行.又或页面 加载时table数据是通过ajax从后台获取的.而这时我们想要获取其中的某个值,又该如何获取呢? 如果是要通过某个事 ...

  4. javascript中的promise和deferred:实践(二)

    javascript中的promise和deferred:实践(二) 介绍: 在第一节呢,我花了大量的时间来介绍promises和deferreds的理论.现在呢,我们来看看jquery中的promi ...

  5. Arduino 各种模块篇 摇杆模块

    Arduino的另外几种模块,我们常见的joystick摇杆模块. 用起来很爽,摇杆 有X,Y轴可调 这里有一篇非常想尽的示例代码: http://www.geek-workshop.com/foru ...

  6. PHP之-json转数组,支持多层嵌套json

    function json_to_array($str) { if (is_string($str)) $str = json_decode($str); $arr=array(); foreach( ...

  7. Apache遇到的问题:APR not found

    #./configure --prefix……检查编辑环境时出现: checking for APR... no configure: error: APR not found .  Please r ...

  8. oc之封装与类之间的关系

    1. 面向对象的特征-封装? 封装: 现实生活中的封装: 将很多的小东西 塞在1个大口袋里面. 好处: a. 对外部屏蔽. b. 方便管理. 代码的封装: 函数/方法 就是1种封装的体现: 将一段代码 ...

  9. C语言之变量与常量的介绍

    一 标示符 标识符:可以理解为是变量名.名字常量表示法的常量名,但是不仅限于这两个 命名规范: 1.起名要有意义,基本要做到一看名字就知道是用来干嘛的(要求你遵守,但不会报错,希望能够养成这样的好习惯 ...

  10. SmoOne——开源免费的企业移动OA应用,基于VS.Net

    一.SmoOne是什么一个开源的移动OA应用 二.语言.Net 三.开发环境Visual Studio 四.开发平台Smobiler Designer 五.功能该应用开源代码中包含注册.登录.用户信息 ...