题目1078:二叉树遍历(根据前序和中序遍历结果,获得后序遍历)

来源:互联网 发布:nodejs于java跨域传值 编辑:程序博客网 时间:2024/05/17 06:42
题目描述:

二叉树的前序、中序、后序遍历的定义:
前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树;
中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树;
后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根。
给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯一确定后序遍历)。

输入:

两个字符串,其长度n均小于等于26。
第一行为前序遍历,第二行为中序遍历。
二叉树中的结点名称以大写字母表示:A,B,C....最多26个结点。

输出:

输入样例可能有多组,对于每组测试样例,
输出一行,为后序遍历的字符串。

样例输入:
ABCBACFDXEAGXDEFAG
样例输出:
BCAXEDGAF
</pre>#include<stdio.h><br />#include<string.h><br />#define MAX 50<br /><br /><br />struct Node<br />{<br /><span style="white-space:pre"></span>Node * lChild;<br /><span style="white-space:pre"></span>Node * rChild;<br /><span style="white-space:pre"></span>char c;<br />}na[MAX]; //申请节点所需的内存<br /><br /><br />int alloc;<br />char str1[30],str2[30];<br />Node * create() //创建一个节点<br />{<br /><span style="white-space:pre"></span>na[alloc].lChild=NULL;<br /><span style="white-space:pre"></span>na[alloc].rChild=NULL;<br /><span style="white-space:pre"></span>return &na[alloc++];<br />}<br />Node * build(int s1,int e1,int s2, int e2)<br />{<br /><span style="white-space:pre"></span>Node *ret=create();<br /><span style="white-space:pre"></span>ret->c=str1[s1];<br /><span style="white-space:pre"></span>//printf("创建节点%c\n",ret->c);<br /><span style="white-space:pre"></span>int rootIndex,i;<br /><span style="white-space:pre"></span>for(i=s2;i<=e2;i++) //在后序序列中找到根节点<br /><span style="white-space:pre"></span>if(str2[i]==str1[s1])<br /><span style="white-space:pre"></span>{<br /><span style="white-space:pre"></span>rootIndex=i;<br /><span style="white-space:pre"></span>break;<br /><span style="white-space:pre"></span>}<br /><span style="white-space:pre"></span>if(rootIndex!=s2) //左子树不为空, 左子树节点的个数:rootIndex-s2<br /><span style="white-space:pre"></span>ret->lChild=build(s1+1,s1+(rootIndex-s2),s2,rootIndex-1);<br /><span style="white-space:pre"></span>if(rootIndex!=e2)//右子树不为空<br /><span style="white-space:pre"></span>ret->rChild=build(s1+rootIndex-s2+1,e1,rootIndex+1,e2);<br /><span style="white-space:pre"></span>return ret;<br />}<br /><br /><br />void postOrder(Node *T)<br />{<br /><span style="white-space:pre"></span>if(T->lChild!=NULL)<br /><span style="white-space:pre"></span>postOrder(T->lChild);<br /><span style="white-space:pre"></span>if(T->rChild!=NULL)<br /><span style="white-space:pre"></span>postOrder(T->rChild);<br /><span style="white-space:pre"></span>printf("%c",T->c);<br />}<br /><br /><br />int main()<br />{<br /><span style="white-space:pre"></span>while(scanf("%s%s",&str1,&str2)!=EOF)<br /><span style="white-space:pre"></span>{<br /><span style="white-space:pre"></span>int size1=strlen(str1);<br /><span style="white-space:pre"></span>int size2=strlen(str2);<br /><span style="white-space:pre"></span>alloc=0;<br /><span style="white-space:pre"></span>Node *ret=build(0,size1-1,0,size2-1);<br /><span style="white-space:pre"></span>postOrder(ret);<br /><span style="white-space:pre"></span>printf("\n"); //一定要加上<br /><span style="white-space:pre"></span>}<br /><br /><br />}<br />
参考九度机试教程
0 0