HDU-1710 给出前序中序输出后序

来源:互联网 发布:木托盘设计软件 编辑:程序博客网 时间:2024/05/29 08:45
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

给出中序和前序,输出后序,(这种题得变形可以给出后序和中序,要有中序来确定根节点)

前序是 先根历遍的,所以前序的第一个是根。然后这个根把中序分为两半,左边是左子树,右边是右子树。然后递归

#include<stdio.h>#include<stdlib.h>#include <stack>#include <iostream>using namespace std;typedef struct Tree{    Tree *left;    Tree *right;    int value;}Tree,*t;Tree *root;Tree* create(int *preorder,int *inorder,int n){    Tree *temp;    for(int i=0;i<n;i++)    {        if(preorder[0]==inorder[i])//前者是根,如果在中序找到这个根        {            temp=(Tree*)malloc(sizeof(Tree));//开辟新结点            temp->value=inorder[i];//该结点的值就是找到的当前根的值            temp->left=create(preorder+1,inorder,i);//通过+1把前序序列已经找过的结点移出去,因为是左子树,所以中序指针不变            temp->right=create(preorder+i+1,inorder+i+1,n-i-1);//右子树,所以下一个结点在之前找到的中序结点的右边            return temp;        }    }    return NULL;}void postOrder(Tree *postTree){    /*if(postTree!=NULL)    {        postOrder(postTree->left);        postOrder(postTree->right);        if(postTree==root)//找到最后结束,换行,指针值相比较            printf("%d\n",postTree->value);        else            printf("%d ",postTree->value);    }*/    stack <t> stack;//非递归    Tree *p=postTree;    do    {        while(p!=NULL)        {            stack.push(p);            p=p->left;        }        bool flag=true;        Tree *r=NULL;        while(!stack.empty()&&flag)        {            p=stack.top();            if(p->right==r)            {                if(p==root)                printf("%d\n",p->value);                else                    printf("%d ",p->value);                stack.pop();                r=p;            }            else            {                p=p->right;                flag=false;            }        }    }while(!stack.empty());}int main(){    int n;    int preorder[2010],inorder[2010];    while(scanf("%d",&n)!=EOF)    {        root=NULL;        for(int i=0;i<n;i++)            scanf("%d",&preorder[i]);//输入前序        for(int i=0;i<n;i++)            scanf("%d",&inorder[i]);//输入中序        root=create(preorder,inorder,n);//从前序和中序得到二叉树        postOrder(root);//得到后序遍历序列    }    return 0;}
图析:



//看思想:btree *bulid(btree *pre,btree *in,int n)//给出前序和中递归建立二叉树{    btree *p,*b;    int k;    p=(btree *)malloc(btree);    p->date=pre->date;    for(b=in;b<in+n;++b)        if(b->date==p->date)        break;    k=b-in;//k代表根结点值的那个位置,在下次递归判断的时候判断到k之前的那个节点就可以了    p->lchild=build(pre+1,in,k);    p->rchild=build(pre+k+1,p+1,n-k);//是n-k(因为下次要从前序得第k+1个位置开始进行判断)    return p;        }btree *build(btree *post,btree *in,int n)//后序,中序建立树{    btree *p,*b;    int k;    p=(btree *)malloc(sizeof(btree));    p->date=(post+n-1)->date;    for(b=in;b<in+n;++b)        if(b->date==p->date)        break;    k=b-in;    p->lchild=build(post,in,k);    p->rchild=build(post+k+1,b+1,n-k-1);    //后续:左子树 右子树 根节点 中续:左子树 根节点 右子树     return b;}


阅读全文
1 0
原创粉丝点击