洛谷 P1087FBI树题解--zhengjun
思路
直接\(dfs\),注意要输出后序,即为先左子树再右子树最后根
代码
#include<bits/stdc++.h>
using namespace std;
int n;
string a;
void dfs(int l,int r){
if(l<r){
dfs(l,(l+r)>>1);
dfs(((l+r)>>1)+1,r);
}
int B=0,I=0;
for(int i=l;i<=r;i++){
if(a[i]=='1')I=1;
else B=1;
}
if(I&&B)printf("F");
else if(I)printf("I");
else printf("B");
}
int main(){
scanf("%d",&n);
cin>>a;
dfs(0,a.length()-1);
return 0;
}