HDU 1710 二叉树遍历,输入前、中序求后序
1、HDU 1710 Binary Tree Traversals
2、链接:http://acm.hust.edu.cn/vjudge/problem/33792
3、总结:记录下根结点,再拆分左右子树,一直搜下去。感觉像dfs。
题意:二叉树,输入前、中序求后序。
(1)建立出一颗二叉树,更直观。但那些指针用法并不是很懂。
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std; typedef struct tree
{
tree *l,*r;
int num;
};
tree *root; tree *build(int *a,int *b,int n)
{
tree *s;
for(int i=;i<n;i++)
{
if(a[]==b[i]){
s=(tree *)malloc(sizeof(tree)); //不要漏了这个 s->num=b[i];
s->l=build(a+,b,i);
s->r=build(a+i+,b+i+,n-i-);
return s;
}
} return NULL;
} void postorder(tree *ro)
{
if(ro==NULL)return ;
postorder(ro->l);
postorder(ro->r);
if(ro==root){
printf("%d\n",ro->num);
}
else {
printf("%d ",ro->num);
}
} int main()
{
int n;
int a[],b[];
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
scanf("%d",&b[i]); root=build(a,b,n);
postorder(root);
} return ;
}
(2)直接在遍历时输出。
#include<iostream>
#include<cstdio>
using namespace std; void preorder(int *a,int *b,int n,int flag)
{
for(int i=;i<n;i++)
{
if(a[]==b[i]){
preorder(a+,b,i,);
preorder(a+i+,b+i+,n-i-,);
if(flag){
printf("%d\n",a[]);
}
else {
printf("%d ",a[]);
}
}
}
return ;
} int main()
{
int n,a[],b[];
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
scanf("%d",&b[i]); preorder(a,b,n,);
} return ;
}
HDU 1710 二叉树遍历,输入前、中序求后序的更多相关文章
- HDU 1710 二叉树遍历
首先.先序遍历是先访问根节点.然后左节点 然后右节点.从根节点开始 直到它的子节点没有左节点才开始回溯访问上一个节点的右节点.同理.中序遍历 先访问左节点 然后是父节点 然后是右节点.从根节点开始 直 ...
- UVa 二叉树重建(先序+中序求后序)
题意是给出先序和中序,求出后序. 先序遍历先访问根结点,通过根结点可以在中序中把序列分为左子树部分和右子树部分,我建了一个栈,因为后序遍历最后访问根结点,所以把每次访问的根结点放入栈中.因为后序遍历先 ...
- Tree Recovery(前序中序求后序)
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14640 Accepted: 9091 De ...
- PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟
1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...
- python实现根据前序与中序求后序
我就不板门弄斧了求后序 class Tree(): def __init__(self,x): self.value=x self.left=None self.right=None class So ...
- HLG2040二叉树遍历已知前中,求后
二叉树的遍历 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 60(34 users) Total Accepted: 34(30 us ...
- c++树,知道前序和中序求后序遍历
经常有面试题就是知道一棵树的前序遍历和中序遍历让你写出后序遍历,这个慢慢画是能画出来的,但是要很快的弄出来还是要懂原理. 首先说一下三种遍历:所谓的前序后序和中序都是遍历时遍历根节点的顺序.子树的话依 ...
- 【美国血统 American Heritage 题解】已知前序中序 求后序
题目: 题目名称:美国血统 American Heritage 题目来源:美国血统 American Heritage ## 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的 ...
- ACM题目————已知前序和中序求后序
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ], z ...
随机推荐
- 用with实现python的threading,新鲜啊
哈哈,2.5以后可用.自动加锁释放,如同操作文件打开关闭一样. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading impor ...
- Introduction to replication 翻译
翻译自用,还有很多谬误之处,敬请甄别,转载请注明出处 Introduction to replication (replication介绍) Replication is one of the m ...
- Linux环境下使用C/C++编写CGI(httpd)
step1下载: ftp://ftp.gnu.org/gnu/cgicc/ step2: tar xzf cgicc-X.X.X.tar.gz(用最新版本) cd cgicc-X.X.X ./conf ...
- JavaScript - UnderScore
UnderScore 第一步 call(this) (function() {}.call(this)); 一些简单的初始化操作 (function() { var root = this; var ...
- android 入门-android自定义控件
第一种:继承View 实现自己的属性 <com.cc.imagewithmarkersample.MyView android:id="@+id/myviewid" andr ...
- linux文本模式下使用PPPOE拨号ADSL上网的方法
转自:http://www.myzhenai.com.cn/post/945.html 转载请注明出处:http://www.myzhenai.com/thread-15431-1-1.html ht ...
- 调整vbox虚拟机下的linux全屏模式及分辨率
>>Step1 在VirtualBox菜单栏中选择[设备]->[安装增强功能] >>Step2 点击右上角的[齿轮]图标,然后选择[Log Out],重新登录即可 lin ...
- Iphone [Tab Bar实现多view切换,Picker,DataPicter实现
用Tab Bar Controller处理IPhone多个view切换, 而且还附有创建空项目,picker和DataPicker的实现! 具体步骤: 1.创建一个空项目,选择User Interfa ...
- UVA 12901 Refraction 数学
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83008#problem/E Description HINT 题意: 给你一个 ...
- [Windows驱动开发](四)内存管理
一.内存管理概念 1. 物理内存概念(Physical Memory Address) PC上有三条总线,分别是数据总线.地址总线和控制总线.32位CPU的寻址能力为4GB(2的32次方)个字 ...