【二叉树】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\n");
        return;
    }
    bool flag=isComplete(root);
    if(!flag){
        printf("not complete\n");
        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\n",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;
}

原文地址:https://www.cnblogs.com/itcsl/p/9175785.html

时间: 2024-10-18 01:27:03

【二叉树】hdu 1622 Trees on the level的相关文章

hdu 1622 Trees on the level

题意:按层次遍历二叉树 思路:对于不会建树的同学,直接广搜即可 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <map> #include <string> #include <queue> using namespace std; #define maxn 300 char s[maxn];

UVA122 Trees on the level【二叉树】【BFS】

Trees on the level Background Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in

UVA 122 -- Trees on the level (二叉树 BFS)

 Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为"()"时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个readin()函数,类型设置为bool型,遇到第一种情况时返回true,遇到第二种情况返回false,主程序中只要发现readin返回false时就break,结束整个大循环. 接下来要建立二叉树,首先为二叉树编写一个结构体,然后根据字符串的输入情况来建树,如果是'L'就往左走,走不动时建一颗

UVa122:Trees on the level

Trees on the level Background Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in

122 - Trees on the level(动态分配空间解法)

Trees on the level Background Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in

E - Trees on the level

 Trees on the level  Background Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms i

UVa 122 Trees on the level(建树,层次遍历)

题意  建树并层次遍历输出  (data,pos)  pos表示改节点位置  L代表左儿子  R代表右儿子 建树很简单  开始在根节点  遇到L往左走遇到R往右走 节点不存在就新建   走完了就保存改节点的值  输出直接bfs就行了了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 300; char s[maxn][maxn]; int

UVA 122 Trees on the level 二叉树 广搜

题目链接: https://vjudge.net/problem/UVA-122 题目描述: 给你一种二叉树的构造方法, 让你逐层输出二叉树的节点值, 如果不能够则输出"not complete" 解题思路: 这道题就是硬搞就可以了, 参考紫书去做的, 首先处理输入就是非常麻烦的事情, 用到了sscanf就会轻松很多, 看来C中还是有很多很多的好用的标准库函数可以拿来用的, 例如还有本题中的strchr() , 处理完输入, 然后就去构造数. 然后广搜一遍即可 代码: #include

UVa 122 Trees on the level (动态建树 &amp;&amp; 层序遍历二叉树)

题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻结点之间用一个空格隔开.每棵树的输入用一对空括 号"()"结束(这对括号本身不代表一个结点),注意,如果从根到某个叶结点的路径上有的结点没有在输入中给出,或者给出超过一 次,应当输出not complete.结点个数不超过256. 分析  : 如果使用数组建树的话,256个结点看着不多,但