Binary Tree Traversals 已知先序 中序 求后序
来源:互联网 发布:征服者更新用什么网络 编辑:程序博客网 时间:2024/05/22 05:32
Binary Tree Traversals
纯属个人理解
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8243 Accepted Submission(s): 3720
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.
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
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
Source
HDU 2007-Spring Programming Contest
这里我用两种代码实现 已知是将树还原再遍历,一种是直接输出,都是用的递归思想
第一种:
#include <iostream>
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct node
{
int data;
struct node *l,*r;
}node,*bitree;
void build(bitree &t,int *pre,int *ord,int n)
{
t=new node;
t->data=pre[0];
int i,k;
if(n==0)
{
t=NULL;
return ;
}
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct node
{
int data;
struct node *l,*r;
}node,*bitree;
void build(bitree &t,int *pre,int *ord,int n)
{
t=new node;
t->data=pre[0];
int i,k;
if(n==0)
{
t=NULL;
return ;
}
//a是用来存 左子树的先序 b是中序
int a[1010],b[1010];
i=0;
while(pre[0]!=ord[i])
{
a[i]=pre[i+1];
b[i]=ord[i];
i++;
}
build(t->l,a,b,i);//递归建立左子树
int a[1010],b[1010];
i=0;
while(pre[0]!=ord[i])
{
a[i]=pre[i+1];
b[i]=ord[i];
i++;
}
build(t->l,a,b,i);//递归建立左子树
////c是用来存 右子树的先序 d是中序
int c[1010],d[1010];
int j=0;
for(k=i+1;k<n;k++)
{
c[j]=pre[k];
d[j]=ord[k];
j++;
}
int c[1010],d[1010];
int j=0;
for(k=i+1;k<n;k++)
{
c[j]=pre[k];
d[j]=ord[k];
j++;
}
build(t->r,c,d,j);//递归建立右子树
}
int q=1;
void pr(bitree t)
{
if(t==NULL ) return ;
if(t->l)
pr(t->l);
if(t->r)
pr(t->r);
if(q==1)
{
cout<<t->data;
q=0;
}
else
cout<<" "<<t->data;
}
int pre[1010],ord[1010];
int main()
{
int n;
while(cin>>n)
{
bitree t;
for(int i=0;i<n;i++)
{
cin>>pre[i];
}
for(int i=0;i<n;i++)
cin>>ord[i];
build(t,pre,ord,n);
q=1;
pr(t);
cout<<endl;
}
return 0;
}
第二种:
#include<iostream>
#include<string.h>
using namespace std;
int q[10000],w[10000];
int e;
//已知先序 中序 求后序
void dg(int r1,int l1,int r2,int l2)
{
int i;
if(r1>l1) return;
for(i=r2;w[i]!=q[r1];i++);
dg(r1+1,l1-l2+i,r2,i-1);
dg(l1-l2+i+1,l1,i+1,l2);
if(e==1)
{
cout<<q[r1];e=0;
}
else
cout<<" "<<q[r1];
}
int main()
{
int n;
while(cin>>n)
{
e=1;
for(int i=0;i<n;i++)
cin>>q[i];
for(int i=0;i<n;i++)
cin>>w[i];
dg(0,n-1,0,n-1);
cout<<endl;
}
return 0;
}
#include<string.h>
using namespace std;
int q[10000],w[10000];
int e;
//已知先序 中序 求后序
void dg(int r1,int l1,int r2,int l2)
{
int i;
if(r1>l1) return;
for(i=r2;w[i]!=q[r1];i++);
dg(r1+1,l1-l2+i,r2,i-1);
dg(l1-l2+i+1,l1,i+1,l2);
if(e==1)
{
cout<<q[r1];e=0;
}
else
cout<<" "<<q[r1];
}
int main()
{
int n;
while(cin>>n)
{
e=1;
for(int i=0;i<n;i++)
cin>>q[i];
for(int i=0;i<n;i++)
cin>>w[i];
dg(0,n-1,0,n-1);
cout<<endl;
}
return 0;
}
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
阅读全文
0 0
- Binary Tree Traversals 已知先序 中序 求后序
- hdu 1710 Binary Tree Traversals(由先序和中序建树)
- 已知树的前序和中序求后序 hdu 题目1710 Binary Tree Traversals
- HDU 1710 Binary Tree Traversals(已知先序中序求后序)
- 已知先序,中序,求后序
- POJ2255-Tree Recovery 已知先序遍历和中序遍历求后序遍历
- Tree Recovery 已知先序和中序求后序
- Tree Traversals Again(通过先序、中序遍历在不建树情况下推出后序遍历)
- PAT A1020 Tree Traversals 根据先序和中序求层序
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
- 二叉树——已知先序、中序,求后序
- 已知先序遍历和中序遍历求后序遍历
- 已知先序、中序求后序;已知中序、后序求先序(C++)
- 已知先序遍历和中续遍历求后序遍历
- hdu1710 Binary Tree Traversals ----- 二叉树前序中序推后序
- Binary Tree Preorder Traversal -先序遍历
- HDOJ1710 Binary Tree Traversals
- traversals for Binary Tree
- NYOJ 题目975 关于521
- Spark Core Aggregator
- 最近用到的matlab的一些函数
- 如何在sublime中去掉复制代码而产生的序号
- DNS服务与DHCP服务的设置
- Binary Tree Traversals 已知先序 中序 求后序
- Photoshop加边框脚本
- finally块
- AC自动机
- 套接字实现TCP服务器
- opencv 自带人脸检测模型haarcascade_fromtalface选择对比
- UE4之UDP通信
- 游戏行业的技术化和互动化发展是一大趋势
- matlab的两个复数数组的点乘