Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9283    Accepted Submission(s):
4193

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
 
Source
 
Recommend
lcy   |   We have carefully selected several similar
problems for you:  1708 1707 1709 1509 1512 
 
总结:记录下根结点,再拆分左右子树,一直搜下去。
 #include<iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; typedef struct tree
{
int v;
tree *l, *r;
};
tree *root; tree *build(int *a, int *b, int n)//函数不能少了*
{
tree *s;
int i;
for (i = ; i <= n; i++)
{
if (a[] == b[i])
{
s = (tree*)malloc(sizeof(tree));//开辟空间
s->v = b[i];
s->l = build(a+, b, i-);
s->r = build(a + i, b + i, n - i );
return s;//要记得返回
}
}
return NULL;
} void postorder(tree *ro)
{
if (ro == NULL) return;
postorder(ro->l);
postorder(ro->r);
if (ro == root)
{
printf("%d\n", ro->v);
}
else
{
printf("%d ", ro->v);
}
} int main()
{
int n, a[], b[];
while (cin>>n)
{
int i;
for (i = ; i <=n; i++)
{
cin >> a[i];
}
for (i = ; i <= n; i++)
{
cin >> b[i];
}
root = build(a, b, n);
postorder(root);
}
return ;
}

另一种不建树的方法

 #include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 1111
int n, pre[maxn], in[maxn], post[maxn], id[maxn], res;//pre表示前序遍历序列,in表示中序遍历序列
void print(int a, int b, int c, int d)//a,b,c,d分别表示前序和中序遍历序列的起点和终点
{
int i = id[pre[a]];//根节点
int j = i - c;//中序遍历序列的左子树
int k = d - i;//中序遍历序列的右子树
if (j) print(a + , a + j, c, i - );//左子树非空则递归左子树
if (k) print(a + j + , b, i + , d);//右子树非空则递归右子树
post[res++] = pre[a];
}
int main()
{
while (~scanf("%d", &n))
{
res = ;
for (int i = ; i<n; i++) scanf("%d", &pre[i]);
for (int i = ; i<n; i++) scanf("%d", &in[i]), id[in[i]] = i;
print(, n - , , n - );
for (int i = ; i<n; i++)
printf("%d%c", post[i], i == n - ? '\n' : ' ');
}
return ;
}
 #include <stdio.h>

 static int pre[];
static int mid[]; /**
每次处理数组中的一个小块
特点:先序和后序遍历任意子树都是连续的块
**/
void post(int pre_index, int mid_index, int size, int is_root)
{
if (!size) {
return;
} if (size == )
{
//打印先序
printf("%d ", pre[pre_index]);
return;
} //每个(子)树根的位置
int root; //找到根节点
for (root = ; root < size && pre[pre_index] !=
mid[mid_index + root]; root++); //处理根的左边
post(pre_index + , mid_index, root, );
//处理根的右边
post(pre_index + root + , mid_index + root + ,
size - root - , );
//是否是总根,打印根(相对)
is_root ? printf("%d\n", pre[pre_index]) :
printf("%d ", pre[pre_index]);
} int main()
{
int n, i;
n = ;
while (~scanf("%d", &n))
{
for (i = ; i < n; i++)
scanf("%d", &pre[i]);
for (i = ; i < n; i++)
scanf("%d", &mid[i]);
post(, , n, );
}
return ;
}

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(二叉树)

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

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

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

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

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

  6. HDU 1710 Binary Tree Traversals

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

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

                                                                                Binary Tree Traversals T ...

  8. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

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

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

随机推荐

  1. (转载)ubuntu卸载opencv并重装opencv3.0.0

    ubuntu卸载opencv并重装opencv3.0.0 一. 卸载opencv2.4.9: Going to the "build" folder directory of op ...

  2. python yaml文件读写

    import yaml yaml_dict={"} with open("a.yaml", "w") as f: yaml.safe_dump(yam ...

  3. python 日期的减法

    from datetime import date a = date(,,) b = date(,,) print(b-a)

  4. 【Python】xlrd,NotImplementedError-formatting_info=True not yet implemented

    前言 Python需要读取Excel(.xls..xlsx)时通常使用xlrd模块:如果要对其内容进行编辑的话稍稍有些麻烦,通常的做法是使用xlutils的copy模块对原文件进行复制,然后保存成新的 ...

  5. Java网络编程学习A轮_08_NIO的Reactor模型

    参考资料: 了解 Java NIO 的 Reactor 模型,大神 Doug Lea 的 PPT Scalable IO in Java 必看:http://gee.cs.oswego.edu/dl/ ...

  6. NYOJ 720 DP+二分

    项目安排 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个 ...

  7. C# Lock关键字

    lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁. lock语句根本使用的就是Monitor.Enter和Monitor.Exit,也就是说lock(this) ...

  8. python 多进程——使用进程池,多进程消费的数据)是一个队列的时候,他会自动去队列里依次取数据

    我的mac 4核,因此每次执行的时候同时开启4个线程处理: # coding: utf-8 import time from multiprocessing import Pool def long_ ...

  9. 控制反转(IOC)模式

    控制反转(Inversion of Control):提倡实现松耦合层.组件和类的设计原则,颠倒程序的控制流程.IoC使用分离执行特定问题处理代码的概念: IoC意味着将你设计好的对象交给容器控制,而 ...

  10. Struts2基本使用(一)--在项目中引入Struts2

    Struts2基本使用 在MVC开发模式中,Struts2充当控制器(Controller)的角色.其主要功能就是处理用户请求,生成响应,是连接视图层(View)和模型层(Model)的桥梁.在处理用 ...