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. BZOJ 2342: 【SHOI2011】 双倍回文

    题目链接:双倍回文 回文自动机第二题.构出回文自动机,那么一个回文串是一个“双倍回文”,当且仅当代表这个串的节点\(u\)顺着\(fail\)指针往上跳,可以找到一个节点\(x\)满足\(2len_x ...

  2. hdu 1498 50 years, 50 colors 最小点覆盖

    50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. tp5.1 Env使用

    5.1版本取消了所有的系统常量,原来的系统路径变量改为使用Env类获取(需要引入think\facade\Env) echo "app_path=========".Env::ge ...

  4. InfiniBand 与Intel Omni-Path Architecture

    Intel Omni-Path Architecture (OPA) 是一种与InfiniBand相似的网络架构 可以用来避免以下PCI总线一些缺陷: 1.由于采用了基于总线的共享传输模式,在PCI总 ...

  5. python批量修改ssh密码

    由于工作需要本文主结合了excel表格,对表格中的ssh密码进行批量修改 以下是详细代码(python3): #!/usr/bin/env python#-*-coding:utf-8-*- impo ...

  6. Rails 5 Test Prescriptions 第11章其他部分的测试。

    Routes✅ Helper Methods✅ Controllers and Requests✅ Simulating Requests⚠️,看之前的博客 What to Expect in a R ...

  7. VS2019/VS2017安装源离线下载,更新,清理,企业版与论坛版重复下载

    VS2019 安装器下载 https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel ...

  8. HDU 4522 (恶心建图)

    湫湫系列故事——过年回家 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  9. ASP.NET ValidationSummary 控件

    ASP.NET ValidationSummary 控件 Validation 服务器控件 定义和用法 ValidationSummary 控件用于在网页.消息框或在这两者中内联显示所有验证错误的摘要 ...

  10. 蓝桥杯练习系统历届试题 带分数 dfs

    问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...