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. centos'的yum安装php的memcache扩展

    centos'的yum安装php的memcache扩展 博客分类: linux   让php能使用memcached服务的扩展有两种:memcache 和 memcached 1. 先安装libmem ...

  2. JS获取checkbox的个数

    本文算是转载自网络,当时用了他的函数,现在想总结一下,但忘了原文地址了 ================================================================ ...

  3. 测试LVS+Keepalived高可用负载均衡集群

    测试LVS+Keepalived高可用负载均衡集群 1. 启动LVS高可用集群服务 此时查看Keepalived服务的系统日志信息如下: [root@localhost ~]# tail -f /va ...

  4. php概率算法(转)

    这是一个很经典的概率算法函数: function get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr) ...

  5. C#数字格式化

    格式规范的完整形式:{index [,width][:formatstring]} index是此格式程序引用的格式字符串之后的参数,从零开始计数:width(可选) 是要设置格式的字段的宽度,wid ...

  6. PostgreSQL建立分区表示例

    pgsql 分区表: --主表 create table test(id integer, name varchar(32)); create index idx_test_id on test us ...

  7. 到底instanceof是啥?

    对Js有一定了解的盆友肯定都知道instanceof 并且还很常用,比如说用[1, 2, 3] instanceof Array 来判断是否是数组.所以我们可能会简单的以为他就是一个用来判断typeo ...

  8. sql移除换行回车符号 \r\n

    --移除回车符 update master_location SET street_number = REPLACE(street_number, CHAR(13), '') --移除换行符 upda ...

  9. PHP文件的上传与下载

    文件上传: 1.单个与多个文件上传 2.上传表单的属性设置 3.PHP配置文件中相关文件上传的设置 4.PHP处理上传的文件数据 php.ini配置: file_uploads = on; 默认on ...

  10. 大数据之pig安装

    大数据之pig安装 1.下载 pig download 2. 解压安装 mapreduce模式安装: 1:设置HADOOP_HOME,如果pig所在节点不是集群中的节点,那就需要把集群中使用的hado ...