hdu3999 The order of a Tree
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999
题意:给一序列,按该序列插入二叉树,给出字典序最小的插入方法建相同的一棵树出来。即求二叉树的先序遍历。
#include<bits/stdc++.h>
using namespace std;
struct node
{
int v;
node *left,*right;
}*root;
node* build(node *root,int v)
{
if(root==NULL)
{
root=new node();
root->v=v;
root->left=root->right=NULL;
}
else if(v<=root->v)root->left=build(root->left,v);
else root->right=build(root->right,v);
return root;
} int flag;
void dfs(node *root)
{
if(flag==)cout<<root->v,flag=;
else cout<<" "<<root->v;
if(root->left!=NULL)dfs(root->left);
if(root->right!=NULL)dfs(root->right);
}
int main()
{
int n;
while(cin>>n)
{
flag=;
root=NULL;
int tmp;
for(int i=;i<n;i++)cin>>tmp,root=build(root,tmp);
dfs(root);
cout<<endl;
}
return ;
}
hdu3999 The order of a Tree的更多相关文章
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- 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 ...
- 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 ...
- 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 ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- 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 ...
- 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 ...
- 二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- The order of a Tree
The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...
随机推荐
- 《计算机网络 自顶向下方法》 第8章 计算机网络中的安全 Part2
SSL(使 TCP 连接安全) SSL(Secure Socket Layer),即安全套接字层,是对 TCP 的强化 HTTPS 使用 SSL,而 HTTP 不使用 SSL 通过采用机密性.数据完整 ...
- css字体图标的制作
我介绍的这个办法是直接在 "阿里巴巴图标库"中制作的,方便快捷 1. 首先到 "阿里巴巴图标库"中找到你想要的图片,然后选择加入购物车 接着我们点击右上角的购物 ...
- nyoj 45-棋盘覆盖 (高精度, Java)
棋盘覆盖 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的 ...
- 关于配置Nginx反向代理后SpringSecurity认证失败的问题解决
问题背景 最近在写的一个项目,采用前后端分离的方式进行开发,登录认证使用的是SpringSecurity框架. 问题描述 在项目部署的时候出现了一个问题,在自己电脑上运行的时候一切顺畅,可是部署到服务 ...
- I/O多路复用模型
背景 在文章<unix网络编程>(12)五种I/O模型中提到了五种I/O模型,其中前四种:阻塞模型.非阻塞模型.信号驱动模型.I/O复用模型都是同步模型:还有一种是异步模型. 想写一个系列 ...
- 024.掌握Pod-部署MongoDB
一 前期准备 1.1 前置条件 集群部署:Kubernetes集群部署参考003--019. glusterfs-Kubernetes部署:参考<附010.Kubernetes永久存储之Glus ...
- 《Java基础教程》第一章学习笔记
Java 是什么呀! 计算机语言总的来说分成机器语言,汇编语言,高级语言.其中Java一种高级计算机语言,它是一种可以编写跨平台应用软件,完全面向对象的程序设计语言. Java划分为三个技术平台,Ja ...
- 如何编译和使用自定义Qt动态链接库 | how to build and use user-defined qt library
本文首发于个人博客https://kezunlin.me/post/cf628dd8/,欢迎阅读! guide to build qt library and use in another proje ...
- node mysql+node+express 表查询及接口建立(6)
一.一张表查询 查询一张表在上一章节说过了,查询全部使用*,具体的就写字段名 'SELECT * FROM company' //查询所有使用* 'SELECT * FROM company WHER ...
- vue 中 keep-alive 缓存数据、离开时位置
路由中: 页面中: 需要缓存的组件中: 因为是keep-alive 所以在初始化页面的时候 会走一次生命周期 当二次进入的时候就已经是缓存状态了 不会在走生命周期 于是它就有了自己的周期函数分别是 ...