1. 程式人生 > >題目1078:二叉樹遍歷(根據前序和中序遍歷結果,獲得後序遍歷)

題目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 />
參考九度機試教程