Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3340    Accepted Submission(s): 1500

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
今天刷二叉树专题,搞了半天最终对重建有了一点认识,首先说一下由先序和中序重建二叉树,大体思路是大概是这种:由于先序(pre)的第一个字母肯定是根节点。所以要在中序(ins)序列中找到根节点的位置k,然后递归重建根节点的左子树和右子树。此时要把左子树和右子树当成根节点,然后表示出它们的位置,左子树非常好说,pre+1就是它的位置。对于右子树来说,pre+k+1为它的位置。如图
对于由中序和后序重建,思路跟上面大体一样,详细写法有点区别。这个就要在中序序列中找到后序的最后一个字母(即根节点),然后递归重建左子树和右子树,依然是把左子树和右子树当成根节点。左子树的位置为pos+k-1,但传的參数应该是pos (由于我们要找的字母是pos[n-1]),而右子树的位置在后序的倒数第二个字母。传參数为pos+k。(不是pos+k+1,想明确这个就算真明确了)
另为这篇没写由后序和中序重建的代码,曾经写过,在这点击打开链接
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
using namespace std;
const int INF=1<<27;
const int maxn=5100;
typedef struct node
{
int data;
node *l,*r;
}*T;
void rbuild(T &root,int *pre,int *ins,int n)
{
if(n<=0)
root=NULL;
else
{
root=new node;
int k=find(ins,ins+n,pre[0])-ins;
root->data=pre[0];
rbuild(root->l,pre+1,ins,k);
rbuild(root->r,pre+k+1,ins+k+1,n-k-1);
}
}
int ans[maxn],p;
void last(T root)
{
if(root)
{
last(root->l);
last(root->r);
ans[p++]=root->data;
}
}
int main()
{
int n,i;
while(cin>>n){
T root;int pre[maxn],ins[maxn];
for(i=0;i<n;i++)
cin>>pre[i];
for(i=0;i<n;i++)
cin>>ins[i];
rbuild(root,pre,ins,n);
p=0;
last(root);
for(i=0;i<p;i++)
if(i!=p-1)
cout<<ans[i]<<" ";
else
cout<<ans[i]<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 1710-Binary Tree Traversals(二进制重建)的更多相关文章

  1. hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

    题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...

  2. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. HDU 1710 Binary Tree Traversals(二叉树)

    题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...

  5. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  6. 【二叉树】hdu 1710 Binary Tree Traversals

    acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...

  7. HDU 1710 Binary Tree Traversals

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...

  8. hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

                                                                                Binary Tree Traversals T ...

  9. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  10. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. ARMv8 Linux内核异常处理过程分析

    NOTE:为了方便大家阅读,制作了PDF版文档.下载请猛戳这里 老样子,为了赚点积分下载其它人的文件,下载以上资料须要资源分2分. 假设没有积分请留言全部文档,留下邮箱就可以. 看了Linaro提供的 ...

  2. Android:主题(Theme)

    1.主题和样式的区别主要区别在 主题不能作用于单个View组建,主题应该对整个应用中的所有Activity起作用或者对指定的Activity起作用. 主题定义的格式应该是改变窗口的外观格式,例如窗口变 ...

  3. ecshop首页调用指定分类的所有产品(指定一级调二级)

    第一种方法 第一 在/includes/lib_goods.php下增加如下代码,用过网上的直接换掉就可以 function index_get_cat_id_goods_best_list($cat ...

  4. hdu 5035 概率论

    n服务形式,各服务窗口等候时间指数公布,求所需的等待时间. 解: 相两点:首先,等到轮到他,然后就是送达时间. 潜伏期期望每个表单1/ki(1/ki,宣布预期指数公式).总的等待时间预期1/(求和ki ...

  5. ASP.NET Core环境并运行 继续跨平台

    ASP.NET Core环境并运行 继续跨平台 无需安装mono,在Linux(Ubuntu)下搭建ASP.NET Core环境 继续.NET跨平台 上一篇:使用VS Code开发ASP.NET Co ...

  6. CSDN的SDCC大会(2013)中使用的PPT分享

    SDCC大会今天开完个.呵呵~ PPT下载链接在最后面,对内幕不感兴趣的可以直接无视下面的种种啰嗦直接“嗖”到最后. 这里说说这个大会中我的Topic. 此前CSDN向我约了一个主题,我回复说, 我可 ...

  7. Android开发之Buidler模式初探结合AlertDialog.Builder解说

          什么是Buidler模式呢?就是将一个复杂对象的构建与它的表示分离,使得相同的构建过程能够创建不同的表示.Builder模式是一步一步创建一个复杂的对象,它同意用户能够仅仅通过指定复杂对象 ...

  8. uva 11427 - Expect the Expected(概率)

    题目链接:uva 11427 - Expect the Expected 题目大意:你每天晚上都会玩纸牌,每天固定最多玩n盘,每盘胜利的概率为p,你是一个固执的人,每天一定要保证胜局的比例大于p才会结 ...

  9. 初步C++类模板学习笔记

    类模板 实现:在上课时间的定义给它的一个或多个参数,这些参数代表了不同的数据类型.                              -->抽象的类. 在调用类模板时, 指定參数, 由编 ...

  10. ios发电子邮件

    ios发电子邮件 by 吴雪莹 第一: NSString *myEmail = @"3423423423@qq.com"; NSString *toemail = @"a ...