Hdu–1710–Binary Tree Traversals

來源:互聯網
上載者:User
Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2494    Accepted Submission(s): 1084

Problem DescriptionA 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.

 

InputThe 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. 

OutputFor 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<cstdio>#include<malloc.h>//#include<iostream>//#include<cstdlib>using namespace std;int n,pre[1005],in[1005];typedef struct node{int data,index;node *lchild,*rchild;}Bitree,*Tree;void DFS(Tree &root,int index){if(root==NULL){root=(Tree)malloc(sizeof(Bitree));root->data=in[index];root->index=index;root->lchild=NULL;root->rchild=NULL;return ;}else{if(index<root->index){printf("root->index=%d\n",root->index);if(root->lchild==NULL){printf("root->lchild=%d\n",root->lchild);}DFS(root->lchild,index);}else{DFS(root->rchild,index);}}}void CreateBitree(Tree &root){int i,j,index;root=(Tree)malloc(sizeof(Bitree));for(i=1; i<=n; i++){if(in[i] == pre[1]){root->data = pre[1];root->index = i;root->lchild=NULL;root->rchild=NULL;break;}}index=i;for(i=2; i<=n; i++){for(j=1; j<=n; j++){if(in[j]==pre[i]){printf("index=%d\n",index);if(j<index){DFS(root->lchild,j);}else{DFS(root->rchild,j);}break;}}}}void post(Tree root,int x){if(root==NULL){return ;}post(root->lchild,x+1);post(root->rchild,x+1);if(x==0){printf("%d",root->data);}else{printf("%d ",root->data);}}int main(){int i;while(scanf("%d%",&n)!=EOF){Tree root;for(i=1; i<=n; i++){scanf("%d",&pre[i]);}for(i=1; i<=n; i++){scanf("%d",&in[i]);}CreateBitree(root);post(root,0);printf("\n");}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.