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

#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. c#常用数据结构解析【转载】

    引用:http://blog.csdn.net/suifcd/article/details/42869341 前言:可能去过小匹夫博客的盆油们读过这篇对于数据结构的总结,但是小匹夫当时写那篇文章的时 ...

  2. Optimization Tipss for Multi Vendor eCommerce Software to drive, retain more sales

    1. Make the Registration & Listing simple  - Only if you keep the registration process and produ ...

  3. linux ipc信号量

    ipcs 命令,可以看到当前系统上的共享资源实例 ipcrm 命令,可以删除一个共享资源实例 linux 操作信号量的函数有三个:semget, semop, semctl semget 声明为: # ...

  4. Fragment Touch事件泄露

    当Fragment的栈里面有几个fragment的时候,这个时候如果是几个fragment状态是hide,当你触摸当前fragment的时候,下层的fragment的事件被触发,这是由于Touch事件 ...

  5. PHP.TP框架下商品项目的优化4-优化商品添加表单js

    优化商品添加表单js 思路 1.制作五个按钮 2.下面五个table 3.全部隐藏,点击则显示 4.点击第几个按钮就显示第几个table 具体操作 1.添加按钮 2.添加五个table并添加class ...

  6. 19,Ubuntu安装之python开发

      什么??公司要用Ubuntu(乌班图)?不会用??怎么进行python开发??? 乌班图操作系统下载地址:http://releases.ubuntu.com/18.04/ubuntu-18.04 ...

  7. cocos2d-x 3.0的入门程序:helloworld

    看过了这么多不同方向的应用,发现很多程序入门都是helloworldhelloworld是所有程序员的绝对初恋 先看一下程序的运行结果吧 然后就是他的工程代码 工程的目录有两个 Classes:程序中 ...

  8. Hbase的安装与部署(集群版)

    HBase 部署与使用 部署 Zookeeper 正常部署 $ ~/modules/zookeeper-3.4.5/bin/zkServer.sh start 首先保证 Zookeeper 集群的正常 ...

  9. Ubuntu14.0.4系统如何获取root权限

    Ubuntu14.0.4系统如何获取root权限 | 浏览:9684 | 更新:2014-08-21 10:38 7 分步阅读 本文主要讲解如何简单实用命令获取root权限 工具/原料 Ubuntu1 ...

  10. 网易云深度剖析Kubernetes优化与实践

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 10 月 15 日,聚焦 Kubernetes 中国行业应用与技术落地的首届中国 Kubernetes 用户大会(KEUC)在杭州成功举办.本次大会 ...