HDU 1710 二叉树三种遍历
Binary Tree Traversals
In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.
In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int num=;
struct donser
{
int data;
donser*lson;
donser*rson;
};
donser *root;
donser *creat(int *a,int *b,int n)//da下标从i开始依次找 db下标从j开始向后扫 n向后扫几个
{
donser *ss;
for(int k=;k<n;k++)
{
if(b[k]==a[])
{
ss=new donser;
ss->data=b[k];
ss->lson=creat(a+,b,k);
ss->rson=creat(a+k+,b++k,n-k-);
return ss;
}
}
return NULL;
} void solve(donser *r)
{
if(r!=NULL)
{
solve(r->lson);
solve(r->rson);
if(r!=root) cout<<r->data<<" ";
else cout<<r->data<<endl;
}
} int main()
{
int da[],db[];//前中
while(~scanf("%d",&num))
{
for(int i=;i<num;i++) {scanf("%d",&da[i]);}
for(int i=;i<num;i++) {scanf("%d",&db[i]);}
root=creat(da,db,num);
solve(root);
}
return ;
}
HDU 1710 二叉树三种遍历的更多相关文章
- 基于Java的二叉树的三种遍历方式的递归与非递归实现
		
二叉树的遍历方式包括前序遍历.中序遍历和后序遍历,其实现方式包括递归实现和非递归实现. 前序遍历:根节点 | 左子树 | 右子树 中序遍历:左子树 | 根节点 | 右子树 后序遍历:左子树 | 右子树 ...
 - PTA 二叉树的三种遍历(先序、中序和后序)
		
6-5 二叉树的三种遍历(先序.中序和后序) (6 分) 本题要求实现给定的二叉树的三种遍历. 函数接口定义: void Preorder(BiTree T); void Inorder(BiTr ...
 - java:数据结构(四)二叉查找树以及树的三种遍历
		
@TOC 二叉树模型 二叉树是树的一种应用,一个节点可以有两个孩子:左孩子,右孩子,并且除了根节点以外每个节点都有一个父节点.当然这种简单的二叉树不能解决让树保持平衡状态,例如你一直往树的左边添加元素 ...
 - javase-常用三种遍历方法
		
javase-常用三种遍历方法 import java.util.ArrayList; import java.util.Iterator; import java.util.List; public ...
 - Java中Map的三种遍历方法
		
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
 - Map三种遍历方式
		
Map三种遍历方式 package decorator; import java.util.Collection; import java.util.HashMap; import java.util ...
 - Jquery中each的三种遍历方法
		
Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...
 - Map的三种遍历
		
import java.util.*;/*** Map的三种遍历方式* @author Administrator**/public class m {public static void main( ...
 - Python 列表(List)  的三种遍历(序号和值)方法
		
三种遍历列表里面序号和值的方法: 最近学习python这门语言,感觉到其对自己的工作效率有很大的提升,特在情人节这一天写下了这篇博客,下面废话不多说,直接贴代码 #!/usr/bin/env pyth ...
 
随机推荐
- c++ operator
			
这篇博文是以前很久写的,贴在我的早期一个blog中,今天google一下,发现还真有不少人转载,可惜并不注明出处.那时觉得operator比较好玩.C++有时它的确是个耐玩的东东.operator它有 ...
 - 用arp-scan扫描局域网IP地址
			
1,在安装之前需要安装yum install -y libpcap libpcap-devel如果没有安装yum工具需要用rpm安装如下软件包[root@oradba arp-scan-1.8]# y ...
 - ubuntu分区
			
参考网址:http://jingyan.baidu.com/article/60ccbceb18624464cab197ea.html
 - CodeForces - 699B One Bomb
			
题目地址:http://codeforces.com/contest/699/problem/B 题目大意: 一个矩阵,内容由‘.’和‘*’组成(‘.’ 空,‘*’ 代表墙),墙分布在不同位置,现找出 ...
 - OC-常见错误  方法与函数的区别
			
对象方法: 1,减号 - 2,声明必须写在@interface和@end之间 实现必须写在@implement 和@end之间 3,对象方法只能由对象来调用 4,对象方法归类.对象所有 函数: 函 ...
 - 在yii2验证之前执行一些额外自定义验证
			
<?php $form = ActiveForm::begin([ 'id' => $model->formName(), 'action' => ['/apitools/de ...
 - Intellij Idea中运行tomcat 报內存溢出 解决方案 火龙战士 火龙战士
			
在Run/Debug configuration 的你要运行行的tomcat里面的 vm options里面输入 -server -XX:PermSize=128M -XX:MaxPermSize=2 ...
 - hibernate4中取得connection的方法
			
在hibernate3中,使用了c3p0连接池,尝试了多种办法取得connection对象,以下两种可以使用. Java代码 Connection conn; // 方法1:hibernate4中将 ...
 - 50多条mysql数据库优化建议
			
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存 ...
 - nginx同一iP多域名配置方法
			
文章转自:http://blog.itblood.com/nginx-same-ip-multi-domain-configuration.html 下面的是我本机的配置: server { list ...