题目链接

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

这道题的大致意思就是给你n个数,要求你给这n个数建成一颗二叉树,建树满足:1)如果这树没有结点,就插入第一个数字变为根。2),如果这树有结点,就去比较结点的大小,如果比结点小,就在该节点的左儿子结点,反之,则在右儿子结点处。
最后输出该二叉树的先序遍历。
具体代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100010;
int pre[maxn];
int k;
struct node{int val;node *l,*r;
};
void add(node* &root,int &t){   //插入if(root==NULL){root=new node;root->val=t;root->l=root->r=NULL;}else {if(t>root->val)add(root->r,t);else add(root->l,t);}
}
void preorder(node *root){   //先序遍历if(root!=NULL){pre[k++]=root->val;preorder(root->l);preorder(root->r);}
}
void remove(node *root){   //删树if(root==NULL)return;remove(root->l);remove(root->r);delete root;
}
int main(){int n;while(cin>>n){node *root=NULL;int x;for(int i=1;i<=n;i++){scanf("%d",&x);add(root,x);}k=0;preorder(root);for(int i=0;i<k;i++)printf("%d%c",pre[i],i==k-1?'\n':' ');remove(root);}
}

道阻且长
自己选的路 跪着也要走完

The order of a Tree(模拟BST的建树和访问)相关推荐

  1. hdu3999 The order of a Tree(BST的建立)

    http://acm.hdu.edu.cn/showproblem.php?pid=3999 题意:给你一个序列可以构成一个二叉搜索树,求此二叉搜索树字典序最小的输入序列. 思路:这题只要明确一点就可 ...

  2. The order of a Tree hdu3999 二叉搜索树

    题目链接: hdu3999 The order of a Tree 题意: 给一个二叉搜索树的序列,求输出可以构成这个二叉搜索树的且满足字典序最小的插入序列 找了一个比较具体描述怎么根据序列建立二叉搜 ...

  3. HDU3999 The order of a Tree【二叉搜索树 + 前序遍历】

    The order of a Tree 题意: 给定一个二叉搜索树的插入顺序,要求输出建树之后的前序遍历. 题解: 连续第二天做到二叉搜索树了,真的吹爆这个算法,从二叉树啥也不懂,到简单搜索树直接秒杀 ...

  4. HDU3999-The order of a Tree

    The order of a Tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  5. Flex使用mx:Tree控件创建树(可添加和删除节点)

    Flex使用<mx:Tree>控件创建树的在线运行效果见:http://tongqiuyan.blog.163.com/blog/static/1955453022011927102332 ...

  6. curl php 模拟来源_php 使用curl模拟ip和来源进行访问的实现方法

    对于限制了ip和来源的网站,使用正常的访问方式是无法访问的.本文将介绍一种方法,使用php的curl类实现模拟ip和来源,访问那些限制了ip和来源的网站. 1.设置页面限制ip和来源访问 server ...

  7. php模拟苹果手机访问,php 使用curl模拟ip和来源进行访问的实现方法

    对于限制了ip和来源的网站,使用正常的访问方式是无法访问的.本文将介绍一种方法,使用php的curl类实现模拟ip和来源,访问那些限制了ip和来源的网站. 1.设置页面限制ip和来源访问 server ...

  8. leetcode【102,103】Binary Tree Level Order Traversal Binary Tree Zigzag Level Order Traversal

    问题描述(102): Given a binary tree, return the level order traversal of its nodes' values. (ie, from lef ...

  9. hdu 3999The order of a Tree

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3999 本题为简单二叉排序树,先按排序树创建树,然后先序遍历二叉树,输出的时候最后一个数字后面没有空格. ...

最新文章

  1. 开源医学图像数据集(资源整合)
  2. fullpage 单屏高度超过屏幕高度,实现单屏内可以滚动并解决手机端单屏高度不正确的问题
  3. hdu1960 最小路径覆盖
  4. 俺的房子内有空调冬暖夏凉,房间宽敞气派非凡
  5. CentOS下NTP安装配置
  6. 电子商务商品供应链概论
  7. 文件服务器php源码,php 在服务器上载文件
  8. 机器视觉算法(数据结构)
  9. 迅捷PDF虚拟打印机怎么打印成pdf文件
  10. java图书借阅系统java图书管理系统java书籍借阅系统
  11. 一步一步教你写股票走势图——分时图四(高亮联动)
  12. WIN10系统蓝屏解决方法
  13. 清华领军计划计算机试题,清华大学2017自主招生真题
  14. js获取0-1之间的随机数,获取1-10之间的随机数
  15. 使用XMLSerializer报错java.lang.NoClassDefFoundError: nu/xom/ParentNode
  16. 图解python中的time、datetime模块及他们的时间格式转换(附:格林尼治时间gmt、本地时间、协调世界时utc)
  17. 《大唐姻缘传》(李治篇) 第一章 天妒人眷 皇限将至
  18. linux 安装水星无线网卡驱动,Linux下安装RTL8188CE网卡驱动(Mercury MW150U)
  19. Window11环境变量配置
  20. 基于Java的出租车管理系统设计与实现

热门文章

  1. Christine Everhart
  2. 十天让你网站ip流量从十增至一万
  3. java中父子类同名变量
  4. 改善服务器响应用时,服务器缓存论文,关于改善网络服务器响应速度建议相关参考文献资料-免费论文范文...
  5. 阿语python4-1 django框架v5.0第2.4节 定义模型类
  6. 浅谈c语言中怎么让程序直接结束(待补充)
  7. windowXP系统无法正常访问vue3网页
  8. 为什么我们要面向接口编程?!
  9. 实验2-2-9 计算火车运行时间 (15 分)
  10. 能链科技获评中国最具投资价值企业榜单TOP10