HDU 1710
http://acm.hdu.edu.cn/showproblem.php?pid=1710
题意:给二叉树的先序遍历和中序遍历,确定后序遍历
解法:复习专业课找的一题,根据先序遍历和中序遍历建树,再对树做后序遍历
#include <iostream>
#include <cstdio> using namespace std; struct Tree {
Tree *lc, *rc;
int data;
}; Tree *Create(int *preorder, int *inorder, int n) {
for(int i = ; i < n; i++) {
if(preorder[] == inorder[i]) {
Tree *node = (Tree*)malloc(sizeof(Tree));
node -> data = preorder[];
node -> lc = Create(preorder+, inorder, i);
node -> rc = Create(preorder+i+, inorder+i+, n-i-);
return node;
}
}
return NULL;
} int rt; void PostOrder(Tree *root) {
if(root != NULL) {
PostOrder(root -> lc);
PostOrder(root -> rc);
if(root -> data == rt)
printf("%d\n", root->data);
else
printf("%d ", root->data);
}
} Tree *root;
int preorder[], inorder[]; int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = ; i < n; i++)
scanf("%d", &preorder[i]);
for(int i = ; i < n; i++)
scanf("%d", &inorder[i]);
root = NULL;
rt = preorder[];
root = Create(preorder, inorder, n);
PostOrder(root);
}
return ;
}
HDU 1710的更多相关文章
- HDU 1710 二叉树遍历,输入前、中序求后序
1.HDU 1710 Binary Tree Traversals 2.链接:http://acm.hust.edu.cn/vjudge/problem/33792 3.总结:记录下根结点,再拆分 ...
- HDU 1710 Binary Tree Traversals(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- hdu 1710 二叉树的遍历
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1710 大意:给出一个二叉树的前序和中序,求其后序遍历 ps:1.在写链表时,需要写明typedef str ...
- 【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
随机推荐
- css学习归纳总结(二) 转
原文地址:css学习归纳总结(二) 标签与元素 <p>标签和p元素有什么区别呢?大多数时候他们表示的是同一样东西,但仍有细微的区别.<p>.<div>等指的是HTM ...
- Getting Started with JD Edwards EnterpriseOne Interoperability
Overview Every enterprise holds a forest of branched system spread across a number of business uni ...
- Linux版Matlab R2015b的bug——脚本运行的陷阱(未解决)
0 系统+软件版本 系统:CentOS 6.7 x64, 内核 2.6.32-573.el6.x86_64软件:Matlab R2015b(包括威锋网和东北大学ipv6下载的资源,都测试过) 1 脚本 ...
- 从客户端(txtContent="<p>1</p>")中检测到有潜在危险的 Request.Form 值
输入1也报这个错误, <pages validateRequest="false" 改了也不行,在页头改也不行.到底什么情况呢? 从这个地方找到了答案:http://nt.d ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- [html]head区域编写规范
一.必须加入的标签 1.meta 2.title 3.css 4.字符集 <meta charset="utf-8"/> 二.可加入的标签 1.设定网页到期时间,一旦网 ...
- Redis 3.0.0 集群部署
简述: 1.0.1:redis cluster的现状 目前redis支持的cluster特性 1):节点自动发现 2):slave->master 选举,集群容错 3):Hot reshardi ...
- 初学js
接触时感觉跟前面写网页的差距和大,与c语言很相似.主要学的有: 1.引入js的三种方法:外联,内联,嵌套 2.标识符:第一个字符可以是任意Unicode字母,以及美元符号($)和下划线(_). - 第 ...
- .net 读取Excel文件报错
错误内容 Microsoft Office Excel 不能访问文件“D:\WWWRoot\Website\Test\Excels\Test1.xls”. 可能的原因有: 1 文件名称或路径不存在. ...
- Java JDBCI批量插入数据
智能插入:将整批分批,每一千条提交一次,sql注入(安全,使用软解析,提高效率) sql注入攻击:简单例子 select * from table where name='"+un+&quo ...