zoukankan      html  css  js  c++  java
  • 【C++】满二叉树问题

    /*
    给出一棵满二叉树的先序遍历,有两种节点:字母节点(A-Z,无重复)和空节点(#)。要求这个树的中序遍历。输出中序遍历时不需要输出#。
    满二叉树的层数n满足1<=n<=5。
    
    Sample Input:
    ABC#D#E
    
    Sample Output:
    CBADE
    */
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int M=1024;
    char data[M];
    int layer;
    
    struct node
    {
        char data;
        struct node *l;
        struct node *r;
    };
    
    void build(node * & t,int l)
    {
        if(l>layer)
            return ;
        int i=0;
        while(data[i]=='')
            i++;
        char tc=data[i];
        
        //cout<<i<<endl<<tc<<endl;
        
        data[i]='';
        if(tc=='#')
        {
            t=NULL;
            return ;
        }
        t=new node();
        t->data=tc;
        build(t->l,l+1);
        build(t->r,l+1);
    }
    
    void display(node *t)
    {
        if(t==NULL)
            return ;
        display(t->l);
        printf("%c",t->data);
        display(t->r);
    }
    
    int main()
    {
        /*
        printf("%d
    ",(int)(log(8)/log(2)));
        printf("%f
    ",(log(7)/log(2)));
        */
        memset(data,'',sizeof(data));
        while(scanf("%s",&data))
        {
            //printf("%s
    ",data);
            int n=strlen(data)+1;
            /*
            if(log(n)/log(2)>(int)(log(n)/log(2)))
                layer=(int)(log(n)/log(2))+1;
            else*/
                layer=(int)(log(n)/log(2));
            //printf("%d
    ",layer);
            node *tree;
            build(tree,1);
            display(tree);
        }
        
        return 0;
    }

    tz@HZAU

    2019/3/13

  • 相关阅读:
    php 工厂模式实例
    nginx多虚拟主机配置
    PHP提高编程效率的方法
    PHP 多态
    锁机制之PHP文件锁
    深入认识javascript中的eval函数(转载)
    PHP&MYSQL 常用的一些性能检测
    寒假作业1:问答题
    软件测试基础知识总结
    七种测试驱动模式
  • 原文地址:https://www.cnblogs.com/acm-icpcer/p/10525989.html
Copyright © 2011-2022 走看看