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

#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. JavaScript数组常用的方法

    改变原数组: ※ push,pop,shif,unshift,sort,reverse ※ splice 不改变原数组: ※ concat,join→split,toString,slice push ...

  2. get请求中文乱码问题

    Get中文乱码解决 Get请求类型: <form action="${pageContext.request.contextPath}/addArtical.action"  ...

  3. C语言进阶—— 接续符和转义符13

    接续符的意义: C语言中的接续符 (\) 是指示编译器行为的利器 我们来看一个案例: #in\ clud\ e <st\ dio.h> in\ t m\ ain(\ ) { pri\ nt ...

  4. python-1基础总结

    输入  >>> name = input() 1--如果字符串里面有很多字符都需要转义,就需要加很多\,为了简化,Python还允许用r''表示''内部的字符串默认不转义,可以自己试 ...

  5. JDK及配置

    Java Jdk开发时环境,程序员使用 Jre运行时环境,用户使用 Jdk的配置 1.新建java_home   jdk的安装路径 例:C:\Program Files (x86)\Java\jdk1 ...

  6. Java中数据类型转换&基本类型变量和对象型变量

    1.Java的数据类型分为三大类 布尔型,字符型和数值型 其中数值型又分为整型和浮点型 2.Java的变量类型 布尔型 boolean 字符型 char 整型    byte,short,int,lo ...

  7. 《Cracking the Coding Interview》——第8章:面向对象设计——题目10

    2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...

  8. dynamic基元类型与隐式类型的局部变量var

    dynamic代码示例 using System; using System.Collections.Generic; using System.Linq; using System.Text; na ...

  9. 社区版pycharm安装Django框架

    1.cmd下执行:pip3 install django 2.cmd下执行:django-admin startproject Demo (Demo为项目名称,可以更改你取的项目名称) 3.cmd下执 ...

  10. jenkins 连接服务器并运行脚本

    1.登录,在系统管理——节点管理——新增节点——配置从节点,添加远程工作目录,选择启动方式:通过JAVA WEB启动代理,添加JDK 2.在列表点节点,点launch下载插件,放到D:\JENKINS ...