Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
搞不懂为什么WA了,反正自己测试正确,大家帮忙看看#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { char ch; struct node * left; struct node * right; }; int find(char in[40],char ch){ int i; for(i=0;i<strlen(in);i++) if(in[i]==ch) return 1; return 0; } struct node * recovery(char *preorder,char *inorder,int n,struct node * root){ int i,j,mid; char pre[40],in[40]; memset(pre,'\0',40); memset(pre,'\0',40); if(strlen(preorder)==0) return NULL; root->ch=preorder[0]; root->left=(struct node *)malloc(sizeof(struct node)); root->right=(struct node *)malloc(sizeof(struct node)); for(i=0;i<strlen(inorder);i++) if(preorder[0]==inorder[i]) break; for(j=0;j<i;j++) in[j]=inorder[j]; j=0; mid=i; if(strlen(inorder)==0) root->left=NULL; else{ for(i=1;i<strlen(preorder);i++) if(find(in,preorder[i])==1) pre[j++]=preorder[i]; root->left=recovery(pre,in,n,root->left); } memset(pre,'\0',40); memset(pre,'\0',40); j=0; for(i=mid+1;i<strlen(inorder);i++) in[j++]=inorder[i]; j=0; if(strlen(inorder)==0) root->right=NULL; else{ for(i=1;i<strlen(preorder);i++) if(find(in,preorder[i])==1) pre[j++]=preorder[i]; root->right=recovery(pre,in,n,root->right); } return root; } void search(struct node * root){ if(root==NULL) return; else { search(root->left); search(root->right); printf("%c",root->ch); return ; } } int main() { struct node root; char preorder[40],inorder[40]; while(scanf("%s%s",preorder,inorder)!=EOF){ recovery(preorder,inorder,0,&root); search(&root); printf("\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator