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/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1361    Accepted Submission(s): 695
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.
1 3 4 2
题解:题目意思即,给你一个插入数列,形成一棵二叉树,你给出字典序最小的插入方法建相同的一棵树出来。说白了,就是求先序序列。
代码:
#include <fstream>
#include <iostream> using namespace std; typedef struct Node{
Node *lch,*rch,*nex;
int x;
Node(int x){
this->x=x;
lch=NULL;
rch=NULL;
}
}inode; int n,tn;
inode *head; void insert(int t);
void preOrder(inode *p); int main()
{
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int t;
while(~scanf("%d",&n)){
scanf("%d",&t);
head=new inode(t);
for(int i=;i<n;i++){
scanf("%d",&t);
insert(t);
}
tn=;
preOrder(head);
}
return ;
}
void insert(int t){
inode *p=head,*s=new inode(t);
while(p!=NULL){
if(t<p->x)
if(p->lch!=NULL) p=p->lch;
else{
p->lch=s;
break;
}
else
if(p->rch!=NULL) p=p->rch;
else{
p->rch=s;
break;
}
}
}
void preOrder(inode *p){
if(p!=NULL){
printf("%d",p->x);
if(++tn<n) printf(" ");
else printf("\n");
preOrder(p->lch);
preOrder(p->rch);
delete p;
}
}
hdu3999-The order of a Tree (二叉树的先序遍历)的更多相关文章
- hdu3999 The order of a Tree
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 题意:给一序列,按该序列插入二叉树,给出字典序最小的插入方法建相同的一棵树出来.即求二叉树的先序 ...
 - [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
		
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
 - [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
		
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
 - [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
		
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
 - lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
		
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
 - LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
		
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
 - [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
		
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
 - LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
		
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
 - LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
		
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
 - LintCode-67.二叉树的中序遍历
		
二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...
 
随机推荐
- Maven中plugins和pluginManagement的区别
			
pluginManagement是表示插件声明,即你在项目中的pluginManagement下声明了插件,Maven不会加载该插件,pluginManagement声明可以被继承. pluginMa ...
 - tidb 安装试用&&以及安装几个问题解决
			
备注: tidb 听说已经很长时间了,一直无安装部署(主要是不像cockrouchdb 不见那么简单) 1. 环境准备(官方建议使用6台机器) // 我的机器准备(阿里云的,同时大家最好选择 ...
 - 新版eclipse编写Android 时常常报的错误 -support-v7
			
一..和以往一样新建完一个Android项目之后.会报android.support.v4.. . . 或者V7找不到的错误. 这是由于这两个包在还有一个附带的项目(android-support ...
 - 如何批处理多个MySQL文件
			
@echo off CHCP 65001 --设置cmd编码for %%i in (E:\sql\*.sql) do ( --多个MySQL SQL文件的存放目录echo excute %%i ...
 - 【转】linux内核态和用户态的区别
			
原文网址:http://www.mike.org.cn/articles/linux-kernel-mode-and-user-mode-distinction/ 内核态与用户态是操作系统的两种运行级 ...
 - mysql插入中文出错,提示1366 - Incorrect
			
提示这样 1366 - Incorrect string value: '\xC0\x86' for column 'platecl' at row 1 是因为编码的问题(在这儿我们需要的是gbk,当 ...
 - 老齐python-基础8(函数)
			
1.函数基本概念 2.理解函数 python中一个函数,就是一种映射关系 3.定义函数 #!/usr/bin/env python #coding:utf-8 def add_function(a,b ...
 - centos如何使用utc时间
			
1.将本地时间文件改名,做备份文件为localtime2 mv /etc/localtime /etc/localtime2 2.将UTC文件和本地文件做连接ln -s /usr/share/zone ...
 - Linut ssh sftp服务重启
			
在网上,收了半天,终于找到这个,记录一下~哈~ RedHat Linux 重启SSH /etc/init.d/sshd restart 重启SFTP /etc/init.d/vsftpd restar ...
 - TCP/IP协议:最大传输单元MTU 和 最大分节大小MSS
			
MTU = MSS + TCP Header + IP Header. mtu是网络传输最大报文包. mss是网络传输数据最大值. MTU:maximum transmission unit,最大传输 ...