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.
 

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 ;
    }
//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);//递归建立左子树
////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++;
    }

    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;
}
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
 
阅读全文
0 0
原创粉丝点击