代码语言:javascript复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char data ;
struct node *l,*r;
};
struct node *creat(char *inorder,char *postorder,int len)
{
struct node *root;
if (len <= 0)
return NULL;
int i = 0;
root = (struct node *)malloc(sizeof (struct node ));
root -> data = *(postorder len -1);
for (i = 0; i < len; i )
{
if (inorder[i] == *(postorder len - 1))
break;
}
root -> l = creat(inorder, postorder, i);
root -> r = creat(inorder i 1,postorder i,len - i - 1);
return root;
}
int fin(struct node*root)
{
int h=0;
int d1,d2;
if (root)
{
d1 = fin(root -> l);
d2 = fin(root -> r);
h = d1>d2?d1 1:d2 1;
}
return h;
}
int main ()
{
int t,len;
char inorder[100],postder[100];
struct node *tree;
while (~scanf ("%d",&t))
{
while (t--)
{
scanf ("%s%s",inorder,postder);
len =strlen (postder);
tree=creat(inorder,postder,len);
int h = fin(tree);
printf ("%dn",h);
}
}
return 0;
}