递归

先判断串的类型,再用递归对二叉树进行后序遍历即可

#include <bits/stdc++.h>
using namespace std;

string str;
void search(int l,int r){
    //访问左子树
    //访问右子树
    //输出根结点
    char ty = '0';
    int temp = 0,len = r - l + 1;
    for(int i = l;i <= r;i++)
        temp += (str[i] - '0');
    //判断类型
    if(temp == 0) ty = 'B';
    else if(temp == len) ty = 'I';
    else ty = 'F';
    if(len != 1){
        search(l,l + len/2 - 1);
        search(l + len/2,r);
    }
    cout<<ty;
}

int main(){
    int n = 0;
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cin>>n>>str;
    search(0,(1 << n) - 1);
    return 0;
}