• C/C++数据结构——[NOIP2004]FBI树(二叉树)


    题目描述

    我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。

    FBI树是一种二叉树[1],它的结点类型也包括F结点,B结点和I结点三种。由一个长度为2N的“01”串S可以构造出一棵FBI树T,递归的构造方法如下:

    1. T的根结点为R,其类型与串S的类型相同;

    2. 若串S的长度大于1,将串S从中间分开,分为等长的左右子串S1和S2;由左子串S1构造R的左子树T1,由右子串S2构造R的右子树T2。

    现在给定一个长度为2N的“01”串,请用上述构造方法构造出一棵FBI树,并输出它的后序遍历[2]序列。

    [1] 二叉树:二叉树是结点的有限集合,这个集合或为空集,或由一个根结点和两棵不相交的二叉树组成。这两棵不相交的二叉树分别称为这个根结点的左子树和右子树。

    [2] 后序遍历:后序遍历是深度优先遍历二叉树的一种方法,它的递归定义是:先后序遍历左子树,再后序遍历右子树,最后访问根。

    输入描述:

    第一行是一个整数N(0 <= N <= 10)
    第二行是一个长度为2N的“01”串。

    输出描述:

    一个字符串,即FBI树的后序遍历序列。

    示例1

    输入

    3
    10001011

    输出

    IBFBBBFIBFIIIFF

    备注:

    对于40%的数据,N <= 2;
    对于全部的数据,N<= 10。

    解题代码(C)

    #include
    #include
    #include
    #include
    #define STR_SIZE 1050
    typedef struct treeNode{
        char str[STR_SIZE];
        int len;
        struct treeNode *L_child,*R_child;
    }node;
    
    void LRN(node *root)
    {
        if(root==NULL)return;
        LRN(root->L_child);
        LRN(root->R_child);
        int flag0=0,flag1=0;
        for(int i=0;i<root->len;i++){
            if(root->str[i]=='0'){
                flag0=1;
            }else{
                flag1=1;
            }
        }
        if(flag0==1&&flag1==0){
            printf("B");
        }else if(flag0==0&&flag1==1){
            printf("I");
        }else if(flag0==1&&flag1==1){
            printf("F");
        }
    }
    void copyStr(char *str1,char *str2,int begin,int end)
    {
        int k=0;
        for(int i=begin;i<=end;i++){
            str1[k++]=str2[i];
        }
        str1[k]='\0';
    }
    
    node* createNode(int begin,int end,char *str )
    {
        if(begin>end){
            return NULL;
        }
        node *root=(struct treeNode *)malloc(sizeof(struct treeNode));
        copyStr(root->str,str,begin,end);
        root->len=end-begin+1;
        if(root->len==1){//只存在一位的时候
            root->L_child=root->R_child=NULL;
        }else{
            root->L_child=createNode(begin,(begin+end)/2,str);
            root->R_child=createNode((begin+end)/2+1,end,str);
        }
        return root;
            
    }
    int main()
    {
        int N;
        char str[STR_SIZE];
        while(scanf("%d",&N)!=EOF)
        {
            node *root;
            N=pow(2,N);
            scanf("%s",str);
            root=createNode(0,N-1,str);
            LRN(root);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    解题思路

    简单二叉树,要注意的点无非就是创建二叉树的时候,注意字符串的起始位置和结束的位置,每个节点如果字符串长度大于2就要对半拆分开来,左边的做左子树,右边的一半做右子树,然后继续递归创建,然后后序遍历的时候要注意判断该结点是F结点B结点还是I结点,具体的上面题目描述都有说明,NOIP普及组的题目,难度不大。

  • 相关阅读:
    3D RPG Course | Core 学习日记三:Navigation智能导航地图烘焙
    《深度医疗》—无情的算法如何为医疗带来人性
    java 多线程&使用wait/notify机制实现list.size()等于5时 的线程销毁——65
    0915 理论知识
    Python高级技巧
    《敏捷无敌之DevOps时代》读后感
    webpack优化篇(五十):使用动态 Polyfill 服务
    【Rust】使用HashMap解决官方文档中的闭包限制
    06 【生命周期 模板引用】
    AWS无服务器 应用程序开发—第九章 权限(Amazon IAM)
  • 原文地址:https://blog.csdn.net/weixin_44546342/article/details/125887076