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 ...
随机推荐
- lqb 入门训练 A+B问题
入门训练 A+B问题 时间限制:1.0s 内存限制:256.0MB 问题描述 输入A.B,输出A+B. 说明:在“问题描述”这部分,会给出试题的意思,以及所要求的目标. 输入格式 输入的第 ...
- python:类3——魔法方法
一.魔法方法特点 被双上下滑线包围 魔法方法是面向对象的Python的一切,如果你不知道魔法方法,说明你还没能意识到面向对象的Python的强大(不是说Python脚本) 通过对制定方法的重写,完全可 ...
- 在input输入值改变reducer里的值
输入值改变reducer里的值 通过store.dispatch传入reducer中,函数的第二个参数可以接收到 在reducer中 在todolist文件中 然后在把this.state中的值改变
- (六)OpenStack---M版---双节点搭建---Neutron安装和配置
↓↓↓↓↓↓↓↓视频已上线B站↓↓↓↓↓↓↓↓ >>>>>>传送门 1.创建网络服务数据库 2.获得 admin 凭证来获取只有管理员能执行的命令的访问权限 3.创 ...
- 揭秘String类型背后的故事——带你领略汇编语言魅力
字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s=“a1a2···an”(n>=0).它是编程语言中表示文本的数据类型.在程序设计中,字符串(string)为符号或数 ...
- 一个HTML5培训班毕业生的找工作随笔
昨天刚参加完一个面试,通过了.写个随笔记录一下. 先介绍一下背景. 我是今年十月份的时候从某个培训机构的HTML5 Web前端培训班毕业的,是一个刚进入IT行业的新人. 本人毕业于某三流学校,在参加培 ...
- JAVA中数组Arrays类的常见用法
import java.util.Arrays; int[] array1={7,8,3,2,12,6,5,4}; 1. //克隆clone int[] array2=array1.clone() ...
- Makefile使用指南
转载请标明出处:http://blog.csdn.net/shensky711/article/details/52231202 本文出自: [HansChen的博客] 什么是Makefile Mak ...
- Java基础面试题及答案(六)
异常 74. throw 和 throws 的区别? throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是 ...
- Maven搭建SpringMvc
Maven搭建SpringMvc,只需跟着一步步操作 项目结构 1 创建Maven项目 index,jsp报错不用管,配置完pom就好了,也可以直接删除掉 2 pom.xml添加依赖 <depe ...