#include<stdio.h>
#include<string.h>
using namespace std;
typedef struct no
{
char data;
struct no *lc,*rc;
} node;
char a[50],b[50];
node *creat(char *x,char *z,int k)
{
node *p;
p=new node;
if(k==0)
{
return NULL;
}
else
{
p->data=z[k-1];
int m=strchr(x,z[k-1])-x;
p->lc=creat(x,z,m);
p->rc=creat(x+m+1,z+m,k-m-1);
}
return p;
}
int high(node *p)
{
int ld,rd;
if(!p)
{
return 0;
}
else
{
ld=high(p->lc);
rd=high(p->rc);
if(ld>rd)
{
return ld+1;
}
else
{
return rd+1;
}
}
}
int main()
{
node *r;
int t,k;
r=new node;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",a,b);
k=strlen(a);
r=creat(a,b,k);
printf("%d\n",high(r));
}
return 0;
}
求二叉树的深度
最新推荐文章于 2020-10-18 16:42:58 发布