#include <bits/stdc++.h>
using namespace std;
struct node
{
char data;
node *right, *left;
};
int lenth, k;
char str1[60], str2[60];
node *creat();
int high(node *root);//二叉树高度
node *build(int low, int high);//还原二叉树
int main()
{
int n;
while(cin >> n)
{
k = 0;
scanf("%s", str1);
scanf("%s", str2);
lenth = strlen(str2);
node *root = build(0,n-1);
cout << high(root) << endl;
}
return 0;
}
node *creat()
{
node *root = new node;
root -> left = NULL;
root -> right = NULL;
return root;
}
int high(node *root)
{
int highleft, highright;
if(root)
{
highleft = high(root -> left);
highright = high(root -> right);
return max(highleft, highright)+1;
}
else return 0;
}
node *build(int low, int high)
{
int flag;
if(low > high)
return NULL;
for(int i = low; i <= high; i++)
{
if(str1[k] == str2[i])
{
flag = i;
break;
}
}
k++;
node *root = creat();
root -> data = str2[flag];
root -> left = build(low, flag-1);
root -> right = build(flag+1,high);
return root;
}
SDUT 3343 数据结构实验之二叉树四:还原二叉树
最新推荐文章于 2021-06-25 19:14:25 发布