Binary Tree Traversals

Problem Description
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

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.

 
Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
 
Output
For each test case print a single line specifying the corresponding postorder sequence.
 
Sample Input
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
 
Sample Output
7 4 2 8 9 5 6 3 1
 
通过先序中序序列求出后序遍历序列,递归玩的不熟,树是时候系统学习一下了。
#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 二叉树三种遍历的更多相关文章

  1. 基于Java的二叉树的三种遍历方式的递归与非递归实现

    二叉树的遍历方式包括前序遍历.中序遍历和后序遍历,其实现方式包括递归实现和非递归实现. 前序遍历:根节点 | 左子树 | 右子树 中序遍历:左子树 | 根节点 | 右子树 后序遍历:左子树 | 右子树 ...

  2. PTA 二叉树的三种遍历(先序、中序和后序)

    6-5 二叉树的三种遍历(先序.中序和后序) (6 分)   本题要求实现给定的二叉树的三种遍历. 函数接口定义: void Preorder(BiTree T); void Inorder(BiTr ...

  3. java:数据结构(四)二叉查找树以及树的三种遍历

    @TOC 二叉树模型 二叉树是树的一种应用,一个节点可以有两个孩子:左孩子,右孩子,并且除了根节点以外每个节点都有一个父节点.当然这种简单的二叉树不能解决让树保持平衡状态,例如你一直往树的左边添加元素 ...

  4. javase-常用三种遍历方法

    javase-常用三种遍历方法 import java.util.ArrayList; import java.util.Iterator; import java.util.List; public ...

  5. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  6. Map三种遍历方式

    Map三种遍历方式 package decorator; import java.util.Collection; import java.util.HashMap; import java.util ...

  7. Jquery中each的三种遍历方法

    Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...

  8. Map的三种遍历

    import java.util.*;/*** Map的三种遍历方式* @author Administrator**/public class m {public static void main( ...

  9. Python 列表(List) 的三种遍历(序号和值)方法

    三种遍历列表里面序号和值的方法: 最近学习python这门语言,感觉到其对自己的工作效率有很大的提升,特在情人节这一天写下了这篇博客,下面废话不多说,直接贴代码 #!/usr/bin/env pyth ...

随机推荐

  1. c++ operator

    这篇博文是以前很久写的,贴在我的早期一个blog中,今天google一下,发现还真有不少人转载,可惜并不注明出处.那时觉得operator比较好玩.C++有时它的确是个耐玩的东东.operator它有 ...

  2. 用arp-scan扫描局域网IP地址

    1,在安装之前需要安装yum install -y libpcap libpcap-devel如果没有安装yum工具需要用rpm安装如下软件包[root@oradba arp-scan-1.8]# y ...

  3. ubuntu分区

    参考网址:http://jingyan.baidu.com/article/60ccbceb18624464cab197ea.html

  4. CodeForces - 699B One Bomb

    题目地址:http://codeforces.com/contest/699/problem/B 题目大意: 一个矩阵,内容由‘.’和‘*’组成(‘.’ 空,‘*’ 代表墙),墙分布在不同位置,现找出 ...

  5. OC-常见错误 方法与函数的区别

    对象方法: 1,减号 - 2,声明必须写在@interface和@end之间   实现必须写在@implement 和@end之间 3,对象方法只能由对象来调用 4,对象方法归类.对象所有 函数: 函 ...

  6. 在yii2验证之前执行一些额外自定义验证

    <?php $form = ActiveForm::begin([ 'id' => $model->formName(), 'action' => ['/apitools/de ...

  7. Intellij Idea中运行tomcat 报內存溢出 解决方案 火龙战士 火龙战士

    在Run/Debug configuration 的你要运行行的tomcat里面的 vm options里面输入 -server -XX:PermSize=128M -XX:MaxPermSize=2 ...

  8. hibernate4中取得connection的方法

    在hibernate3中,使用了c3p0连接池,尝试了多种办法取得connection对象,以下两种可以使用. Java代码  Connection conn; // 方法1:hibernate4中将 ...

  9. 50多条mysql数据库优化建议

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存 ...

  10. nginx同一iP多域名配置方法

    文章转自:http://blog.itblood.com/nginx-same-ip-multi-domain-configuration.html 下面的是我本机的配置: server { list ...