集训笔记---二叉树应用(HDUOJ NO.1710 Binary Tree Traversals)

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

这是一个已知二叉树先序,中序序列,求解二叉树后续序列的题目;

先序:根左右

中序:左根右

后序:左右根

那么这其实可以用一个递归函数写出来嘛

下面是AC代码

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
struct edge
{
    int date;
    edge* left;
    edge* right;
};
int c[1000], j;
void the_tree(int* ino, int* pre, int len);
int main(void)
{
    int len, i;
    int pre[1009], ino[1009];
    while(scanf("%d", &len) != EOF)
    {
        for(i = 0; i < len; i++)
        {
            scanf("%d", &pre[i]);
        }
        for(i = 0; i < len; i++)
        {
            scanf("%d", &ino[i]);
        }
        j = 0;
        the_tree(ino, pre, len);
        for(i = 0; i < len; i++)
        {
            if(i == 0)
            {
                printf("%d", c[i]);
            }
            else
            {
                printf(" %d", c[i]);
            }
        }
        printf("\n");
    }
}
void the_tree(int* ino, int* pre, int len)
{
    if(len == 0)//递归终止条件 
    {
        return ;
    }
    edge* node = new edge;//new相当于molloc,动态分配内存 
    //使用edge* node = new edge;这行语句,为变量node分配了一个空间,用来存储节点信息 
    node->date = *pre;//首先确定树(子树)根节点的储存的内容 
    int root;//循环查找中序序列中根节点的位置 
    //以便将整个树从根节点分为左右子树 
    for(root = 0; root < len; root++)
    {
        if(ino[root] == *pre)
        {
            break;
        }
    }
    the_tree(ino, pre+1, root);//第一个递归,pre+1这波移位,让我们从根节点到了左子树 
    //而对于ino由于len变成了root,也让我们从根节点到了左子树
    the_tree(ino+root+1, pre+root+1, len-root-1);//第二个递归,同样的移位,让我们得到了pre右子树
    //移位加上len的变化使我们得到了ino右子树 
    c[j] = node->date;
    j++;
}

猜你喜欢

转载自blog.csdn.net/zzuli_xiaomingke/article/details/81411341
今日推荐