洛谷 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;
}

谢谢--zhengjun

posted @ 2022-06-11 13:37  A_zjzj  阅读(18)  评论(0编辑  收藏  举报