1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<stdio.h> #include<stdlib.h> #include<string.h>
typedef struct TNode { char data; struct TNode *lchild,*rchild; } TNode,*Tree;
Tree CreatTree( char xian[],char zhong[],int n); int High( Tree t);
char xian[55]; char zhong[55]; int n;
int main() { scanf("%d",&n); scanf("%s",xian); scanf("%s",zhong); Tree tree = CreatTree( xian,zhong,n); printf("%d",High(tree)); return 0; }
Tree CreatTree( char xian[],char zhong[],int n) { if( n==0 ) return NULL; int index = 0; Tree temp = (Tree) malloc(sizeof(struct TNode));
while( index < n) { if( zhong[index]==xian[0]) break; index ++; } temp->data = xian[0]; temp->lchild = CreatTree(xian+1,zhong,index); temp->rchild = CreatTree(xian+1+index,zhong+index+1,n-index-1); return temp; }
int High( Tree t) { if( !t ) return 0; int lh = High(t->lchild); int rh = High(t->rchild); if( lh>rh ) return ++lh; else return ++rh; }
|