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. 理解Linux系统/etc/init.d目录和/etc/rc.local脚本

       一.关于/etc/init.d 如果你使用过Linux系统,那么你一定听说过init.d目录.这个目录到底是干嘛的呢?它归根结底只做了一件事情,但这件事情非同小可,是为整个系统做的,因此它非常重 ...

  2. 转:netflix推荐系统竞赛

    原文链接:Netflix recommendations: beyond the 5 stars (Part 1), (Part 2) 原文作者:Xavier Amatriain and Justin ...

  3. 自然语言20.1 WordNet介绍和使用 _

    http://blog.csdn.net/ictextr9/article/details/4008703 Wordnet是一个词典.每个词语(word)可能有多个不同的语义,对应不同的sense.而 ...

  4. 《JavaScript DOM编程艺术》笔记一

    1.CSS: 继承是CSS技术中的一项强大功能,节点树上的各个元素将继承其父元素的样式属性. 2.3种获取DOM元素方法:getElementById返回一个对象,getElementsByTagNa ...

  5. 数字图像处理- 3.4 空间滤波 and 3.5 平滑空间滤波器

    3.4 空间滤波基础 • Images are often corrupted by random variations in intensity, illumination, or have poo ...

  6. ecshop 团购-》调取评论

    涉及到的文件及代码:lib_insert.php,comments.lbi,{insert name='comments' type=$type id=$id} html代码: <blockqu ...

  7. Django笔记-常见错误整理

    1.csrf错误 解决方法:在settings.py里注释掉相关内容即可 MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.Sess ...

  8. Lua 之os库

    标准os库 os.rename(oldname, newname) 文件重命名: os.remove(filename) 删除一个文件 os.execute(cmd) os.execute可运行一条系 ...

  9. vim配色方案设置(更换vim配色方案)

    vim配色后,我的 设定底色为黑色,字体为绿色,然后将文件夹设为洋红,默认的注释换为淡黄:其实有一种简单的方法,就是设定为系统配置好的配色方案:转载文章如下:   ---------------- ( ...

  10. linuxMint install shuruma

    http://my.oschina.net/u/1446273/blog/306053 http://blog.sina.com.cn/s/blog_5d406a1b0101jlgn.html htt ...