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

题目描述:

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

输入:

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

输出:

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

样例输入:
ABC
BAC
FDXEAG
XDEFAG
样例输出:
BCA
XEDGAF
</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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值