Binary Tree Traversals (树的重建)

来源:互联网 发布:网线接入网络方式 编辑:程序博客网 时间:2024/04/29 09:22
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
代码1
#include <iostream>using namespace std;int in[1005];int pre[1005];int pos=0;int n;void dfs(int a, int b, int len){    if (len == 1)    {        cout << pre[a];        if(pos==n-1)            cout<<'\n';        else cout<<' ';        pos++;        return;    }    else if (len <= 0)        return;    int i;    for (i=0; pre[a] != in[b + i]; i++);    dfs(a+1,b, i);//左子树    dfs(a + 1 + i, b + i + 1, len - i - 1);//右子树    {        cout << pre[a] ;        if(pos==n-1)            cout<<'\n';        else cout<<' ';        pos++;    }}int main(){    while (cin >> n)    {        pos=0;        for (int i = 1; i <= n; i++)            cin >> pre[i];        for (int i = 1; i <= n; i++)            cin >> in[i];        dfs(1, 1, n);        //cout << endl;    }}


代码2
#include<iostream>#include<string.h>#include<algorithm>#include<vector>#include<stdio.h>using namespace std;int n,pos=0;vector<int>pre,in,post;void rec(int l,int r){    if(l>r)    {        return ;    }    int root=pre[pos++];    int m=distance(in.begin(),find(in.begin(),in.end(),root));    rec(l,m-1);    rec(m+1,r);    post.push_back(root);}void solve(){    rec(0,pre.size()-1);    for(int i=0; i<n; i++)    {        printf("%d ",post[i]);    }    printf("\n");    return ;}int main(){    while(cin>>n)    {        int a;        pos=0;        pre.clear();        in.clear();        post.clear();        for(int i=0; i<n; i++)        {            cin>>a;            pre.push_back(a);        }        for(int i=0; i<n; i++)        {            cin>>a;            in.push_back(a);        }        solve();    }    return 0;}


0 0
原创粉丝点击