不用那么麻烦 做个函数判断类型,再按照“左右根"的顺序分解即可 直接上代码吧 12345678910111213141516171819202122#include<bits/stdc++.h>using namespace std;string tree;int n;char get_kind(string x)//获得FBI类型 { if(x.find('0')!=x.npos&&x.find('1')!=x.npos)return 'F'; //find判断是不包含子串要用!=x.npos的形式( x.npos即为没有找到) else if(x.find('0')!=x.npos)return 'B'; else return 'I';}void def(string tree)//“左右根”分解 { if(tree.length()==1){cout<<get_kind(tree);return;} def(tree.substr(0,tree.length()/2));//左 def(tree.substr(tree.length()/2));//右 cout<<get_kind(tree);//根 }int main(){ cin>>n>>tree; def(tree);}