zoukankan      html  css  js  c++  java
  • 【二叉树】hdu 1622 Trees on the level

    【题意】

    给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历

    【思路】

    注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置

    【Accepted】

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    
    int num;
    const int maxn=300;
    char str[maxn];
    struct node{
        int num;
        node *lef;
        node *rig;
    };
    node *root;
    bool tag;
    void bfs(node *rt){
        queue<node *> Q;
        Q.push(rt);
        while(!Q.empty()){
            node *q=Q.front();
            Q.pop();
            if(q==rt){
                printf("%d",q->num);
            }else{
                printf(" %d",q->num);
            }
            if(q->lef){
                Q.push(q->lef);
            }
            if(q->rig){
                Q.push(q->rig);
            }
            free(q);
        }
    }
    bool isComplete(node *rt){
        queue<node *> Q;
        Q.push(rt);
        while(!Q.empty()){
            node *q=Q.front();
            Q.pop();
            if(q->num==-1){
                return false;
            }
            if(q->lef!=NULL){
                Q.push(q->lef);
            }
            if(q->rig!=NULL){
                Q.push(q->rig);
            }
        }
        return true;
    }
    void print(node *root){
        if(tag==false){
            printf("not complete
    ");
            return;
        }
        bool flag=isComplete(root);
        if(!flag){
            printf("not complete
    ");
            return;
        }
        bfs(root);
        puts("");
    }
    int main(){
        root=(node *)malloc(sizeof(node));
        root->num=-1;
        root->lef=NULL;
        root->rig=NULL;
        tag=true;
        while(scanf("%s",str)!=EOF){
            if(!strcmp(str,"()")){
                print(root);
                root=(node *)malloc(sizeof(node));
                root->num=-1;
                root->lef=NULL;
                root->rig=NULL;
                tag=true;
            }else{
                sscanf(&str[1],"%d",&num);
            //    printf("%d
    ",num);
                char *comma=strchr(str,',');
                char *path=comma+1;
                node *now=root;
                for(char *i=path;*i!=')';i++){
                    if(*i=='L'){
                        if(now->lef==NULL){
                            node *nd=(node *)malloc(sizeof(node));
                            nd->num=-1;
                            nd->lef=NULL;
                            nd->rig=NULL;
                            now->lef=nd;
                        }
                        now=now->lef;
                    }else{
                        if(now->rig==NULL){
                            node *nd=(node *)malloc(sizeof(node));
                            nd->num=-1;
                            nd->lef=NULL;
                            nd->rig=NULL;
                            now->rig=nd;
                        }
                        now=now->rig;
                    }
                }
                if(now->num!=-1){
                    tag=false; 
                }else{
                    now->num=num;
                }
            }
        }
        free(root);
        return 0;
    }
  • 相关阅读:
    git rebase命令
    java中HashSet对象内的元素的hashCode值不能变化
    Spring中WebApplicationInitializer的理解
    mysql判断表字段或索引是否存在,然后修改
    mysql存储过程
    判断地图上的点是否在圆形,多边形,区域内
    计算任意多边形的面积、中心、重心
    判断点是否在任意多边形内
    springMvc将对象json返回时自动忽略掉对象中的特定属性的注解方式
    String.format()详细用法
  • 原文地址:https://www.cnblogs.com/itcsl/p/9175785.html
Copyright © 2011-2022 走看看