全部自己写的,希望楼主采纳
#include
#include
#include
#include
using namespace std;
char nodeType(string s);
void substring(string s);
vectorv;
int main()
{
int n;
string s;
cout << "please input n:" << endl;
cin >> n;
while (n<0 || n>10) //n大小不对
{
cout << "input error! please input again:" << endl;
cin >> n;
}
cout << "please input s:" << endl;
cin >> s;
while (s.size() != pow(2, n)) //字符串长度不对
{
cout << "input error! please input again:" << endl;
cin >> s;
}
//将字符串0/1改成B/I,便于求串类型
for (int i=0; i{
if (s[i] == '1')
{
s[i] = 'I';
}
else if(s[i] == '0')
{
s[i] = 'B';
}
else //字符串含有非01字符,退出
{
exit(-1);
}
}
substring(s);
string temp = "";
//容器v中的串类型反向输出
for (i=v.size()-1; i>=0; i--)
{
temp += v[i];
}
cout << temp << endl;
return 0;
}
//递归求s的字串类型,先右字串,后左字串,
//所得类型依次存入vector容器v,最后反向输出即为后序遍历
void substring(string s)
{
v.push_back(nodeType(s)); //s的串类型存入v
if (s.size() > 1)
{
string s1, s2;
//拆分s串
s1 = s.substr(0, s.size()/2);
s2 = s.substr(s.size()/2, s.size()/2);
//递归求子串类型, 先右后左最后反向输出v即为后序遍历
substring(s2);
substring(s1);
}
}
//判断s串的类型(F/B/I)
char nodeType(string s)
{
char type = s[0];
for (int i=1; i{
if (s[i] != type)
{
type = 'F';
break;
}
}
return type;
}
题主捡芝麻丢西瓜作弊,回答已删