知前序遍历与中序遍历 求后序遍历

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
bool fist;
const int maxn=;
struct tree_node
{
int value;
tree_node* leftchild;
tree_node* rightchild;
tree_node()
{
leftchild=NULL;
rightchild=NULL;
}
};
/**
根据中序遍历,前序遍历建树
递归 忽略细节 深入至所有结点建立
*/
tree_node* build_tree(int pre[],int in[],int length)
{
if(length==)return NULL;///终止条件
tree_node* temp = new tree_node;
int pos;
for(pos=;pos<length;pos++)///找到根节点->然后根据中序遍历把左子树和右子树分开
{
if(in[pos]==pre[])break;
}
temp->value=pre[];
temp->leftchild=build_tree(pre+,in,pos);
temp->rightchild=build_tree(pre+pos+,in+pos+,length-pos-);
return temp;
} void postOrder(tree_node* root)
{
if(root!=NULL)
{
postOrder(root->leftchild);
postOrder(root->rightchild);
if(!fist)///根节点输出
{
cout<<root->value;
fist=true;
}
else
cout<<" "<<root->value;
}
}
int main()
{
int n;
int pre[maxn],in[maxn];
while(scanf("%d",&n)==)
{
fist=false;
///input
for(int i=;i<n;i++)scanf("%d",&pre[i]);
for(int i=;i<n;i++)scanf("%d",&in[i]);
///solve
tree_node* tree=build_tree(pre,in,n);
postOrder(tree);
cout<<endl;
}
return ;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ;
int pre[maxn], in[maxn], n, pos[maxn]; void creat(int l, int r, int L, int R) { if (L == R) {
printf("%d ", in[L]);
return;
} int id = pos[pre[l]];///in中父节点位置
if(id > L) creat(l + , l + id - L, L, id - );//边界, 左
if(id < R) creat(l + + id - L, r, id + , R);//右 printf("%d%s", in[id], l == ? "\n" : " ");
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif while (~scanf("%d", &n)) {
for (int i = ; i <= n; ++i) scanf("%d", &pre[i]);
for (int i = ; i <= n; ++i) {
scanf("%d", &in[i]);
pos[in[i]] = i;
}
creat(, n, , n);
} return ;
}

HDU1710---树(知前序遍历与中序遍历 求后序遍历)的更多相关文章

  1. 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现

    public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...

  2. DS Tree 已知先序、中序 => 建树 => 求后序

    参考:二叉树--前序和中序得到后序 思路历程: 在最初敲的时候,经常会弄混preorder和midorder的元素位置.大体的思路就是在preorder中找到根节点(根节点在序列的左边),然后在mid ...

  3. HDU 1710 (二叉树的前序和中序,求后序)

    题目链接 题目大意: 输入二叉树的前序.中序遍历,请输出它的后序遍历 #include <stdio.h> #include <string.h> ; // 长度为n s1 前 ...

  4. hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)

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

  5. HDU 1710Binary Tree Traversals(已知前序中序,求后序的二叉树遍历)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 解题思路:可以由先序和中序的性质得到 : 先序的第一个借点肯定是当前子树的根结点, 那么在 中序 ...

  6. 已知树的前序、中序,求后序的c++实现&已知树的后序、中序,求前序的c++实现

    #include"iostream" using namespace std; int pre[30]; int in[30]; int post[30]; int indexOf ...

  7. TZOJ 3209 后序遍历(已知中序前序求后序)

    描述 在数据结构中,遍历是二叉树最重要的操作之一.所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问. 这里给出三种遍历算法. 1.中序遍历的递归算法定义:  ...

  8. 【美国血统 American Heritage 题解】已知前序中序 求后序

    题目: 题目名称:美国血统 American Heritage 题目来源:美国血统 American Heritage ## 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的 ...

  9. 48. leetcode 105题 由树的前序序列和中序序列构建树结构

    leetcode 105题,由树的前序序列和中序序列构建树结构.详细解答参考<剑指offer>page56. 先序遍历结果的第一个节点为根节点,在中序遍历结果中找到根节点的位置.然后就可以 ...

随机推荐

  1. request中的那些方法到底是干什么的?

    最近做Java Web项目,在.jsp页面和servlet之间request和response还是有些混淆,查阅了一些资料,总结如下,方便以后使用: 首先,servlet接口是最基本的,提供的五个方法 ...

  2. PHP 使用GD库合成带二维码和圆形头像的海报步骤以及源码实现

    PHP 使用GD库合成带二维码和圆形头像的海报步骤以及源码实现 之前记录过一篇文章,不过那只是简单将二维码合成到海报中去,这次还要合成头像,而且是圆形.所需要素材就不一一列举,直接代码吧 1.先获取用 ...

  3. vue 改变我们插值的符号{{}}改为${}

    delimiters的作用是改变我们插值的符号.Vue默认的插值是双大括号{{}}.但有时我们会有需求更改这个插值的形式. delimiters:['${','}'] 现在我们的插值形式就变成了${} ...

  4. 多通道CNN

    在读Convolutional Neural Networks for Sentence Classification 这个文章的时候,它在论文中提出一种模型变种就是 CNN-multichannel ...

  5. 最小生成数kruskal算法和prim算法

    定义 连通图:在无向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该无向图为连通图. 强连通图:在有向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该有向图为强连通图. 连通网:在 ...

  6. C语言进阶——goto 和 void 的分析08

    遭人遗弃的goto: 高手潜规则:禁止使用goto 项目经验:程序质量与goto的出现次数成反比 最后的判决:将goto打入冷宫 程序示例1:(goto副作用分析) #include <stdi ...

  7. Spring Boot :Request请求处理流程

    技术交流群:233513714

  8. 洛谷P1451 求细胞数量

    求细胞数量 题目链接 这道题大概是一个最简单的联通块的题了qwq 注意枚举起点的时候 一定不要从0开始不然你就会从0进入到了其他联通块中从而多查. 一定看清题意这道题不是同色为联通块!!! AC代码如 ...

  9. Win Server 8中的利器:微软在线备份服务

    微软在Windows Server 8中添加在线备份服务了?你一定以为我在开玩笑,是吧?但是微软确实这么做了.     微软在Windows Server 8中添加在线备份服务了?你一定以为我在开玩笑 ...

  10. 网络策略中使用的 VLAN 属性

    TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 按类别提供的 Windows Server 内容 Win ...