#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char a[60], b[60];
typedef struct BiTNode
{
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
BiTNode *createtree(char a[], char b[], int l)
{
char *p;
if(l ==0)
return NULL;
BiTNode *T;
T = new BiTNode;
T->data = b[l-1];
for(p = a; p != '\0'; p++)
{
if(*p == b[l-1])
break;
}
int t = p - a;
T->lchild = createtree(a,b,t);
T->rchild = createtree(p+1,p+1,l-t-1);
return T;
}
int deep(BiTree T)
{
int d = 0;
if(T)
{
int l1 = deep(T->lchild);
int l2 = deep(T->rchild);
d = (l1>l2?l1:l2) + 1;
}
return d;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
while(n--)
{
scanf("%s",a);
scanf("%s",b);
BiTNode *T = new BiTNode;
int l = strlen(a);
T = createtree(a,b,l);
int m = deep(T);
printf("%d\n",m);
}
}
return 0;
}
数据结构实验之二叉树八:(中序后序)求二叉树的深度
最新推荐文章于 2020-07-24 16:17:24 发布