线段树 建树 后序遍历 FBI Tree

https://vjudge.net/contest/360957#problem/B

#include<bits/stdc++.h>
#define endl '\n'
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 1055;
typedef long long ll; 
string s; 
struct node{
    int l,r,type;
}T[N];
string res="";
void Build( int v,int l,int r ){
    T[v].l = l,T[v].r = r; 
    if( l==r ){
        if( s[l]=='0' ) T[v].type = 0;
        else T[v].type = 1;
        return ;
    }
    int mid = (l+r)/2;
    Build( v*2,l,mid );
    Build( v*2+1,mid+1,r );
    if( T[v*2].type==T[v*2+1].type ) T[v].type = T[v*2].type;
    else T[v].type= 2;
}
void aftervis(int v){
    if( T[v].l!=T[v].r ){
        aftervis(v*2);
        aftervis(v*2+1);
    } 
    if( T[v].type==2 ) cout<<'F';
    else if( T[v].type==0 ) cout<<'B';
    else cout<<'I';
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);  
    int t; cin>>t;
    while(t--){
        int n; cin>>n;
        int len = 1<<(n);
        cin>>s; s=" "+s;
        Build( 1,1,len );
        aftervis(1); 
        cout<<endl;
    }
    return 0;
}

 

原文链接: https://www.cnblogs.com/SunChuangYu/p/12488745.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    线段树 建树 后序遍历 FBI Tree

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/335213

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午10:02
下一篇 2023年3月1日 下午10:02

相关推荐