zoukankan      html  css  js  c++  java
  • UVA-122(Trees on the level)

    Trees on the level

    题目链接:

    https://vjudge.net/problem/UVA-122

    题目意思:

    给你一些(,)让你建立一棵树,直到输入()结束建树,然后判断树是否完整,如果没有结点未赋值或者被赋值两次,就按层次遍历输出树,否则输出not complete

    代码:

    #include <algorithm>
    #include <malloc.h>
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    const int maxn=10010;
    using namespace std;
    typedef struct node
    {
        int data;
        struct node *lchild;
        struct node *rchild;
    };
     
    bool judge(node *b)
    {
        queue <node*> q;
        while(!q.empty()) q.pop();
        q.push(b);
        while(!q.empty())
        {
            node *u = q.front();
            q.pop();
            if(u->data < 0) return false;
            if(u->lchild != NULL)q.push(u->lchild);
            if(u->rchild != NULL)q.push(u->rchild);
        }
        return true;
    }
    void LevelOrder(node *b)
    {
        node *p;
        queue <node*> q;
        while(!q.empty())q.pop();
        q.push(b);
        while(!q.empty())
        {
            p = q.front();
            q.pop();
            if(p->data == b->data) printf("%d",p->data);
            else printf(" %d",p->data);
            if(p->lchild!=NULL) q.push(p->lchild);
            if(p->rchild!=NULL) q.push(p->rchild);
        }
        printf("
    ");
    }
    void Destroy(node *&b)
    {
        node *p;
        queue <node*> q;
        while(!q.empty())q.pop();
        q.push(b);
        while(!q.empty())
        {
            p = q.front();
            q.pop();
            if(p->lchild!=NULL) q.push(p->lchild);
            if(p->rchild!=NULL) q.push(p->rchild);
            free(p);
        }
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        node *root;
        root = (node *)malloc(sizeof(node));
        root->lchild = NULL;
        root->rchild = NULL;
        root->data = -1;
        char s[maxn],number[maxn];
        int i,flag=0;
        node *p,*b;
        while(scanf("%s",s)!=EOF)
        {
    //        printf("%s
    ",s);
            if(strcmp(s,"()")!=0)
            {
                int j=0;
                for(i=1; i<strlen(s); i++)
                {
                    if(s[i]==',')break;
                    number[j]=s[i];
                    j++;
                }
                number[j] = '';
                i++;
                p=root;
                while(1)
                {
                    if(s[i]==')')break;
                    else if(s[i]=='L')
                    {
                        if(p->lchild==NULL)
                        {
                            b = (node *)malloc(sizeof(node));
                            p->lchild = b;
                            b->data = -1;
                            b->lchild = NULL;
                            b->rchild = NULL;
                            p=b;
                        }
                        else
                            p = p->lchild;
                    }
                    else if(s[i]=='R')
                    {
                        if(p->rchild==NULL)
                        {
                            b = (node *)malloc(sizeof(node));
                            p->rchild = b;
                            b->data = -1;
                            b->lchild = NULL;
                            b->rchild = NULL;
                            p=b;
                        }
                        else
                            p = p->rchild;
                    }
                    i++;
                }
                if(p->data<0) p->data = atoi(number);
                else flag=1;
            }
            else
            {
                if(judge(root)&&flag!=1)
                {
                    LevelOrder(root);
                    Destroy(root);
                    root = (node *)malloc(sizeof(node));
                    root->lchild = NULL;
                    root->rchild = NULL;
                    root->data = -1;
                    flag=0;
                }
                else
                {
                    printf("not complete
    ");
                    Destroy(root);
                    root = (node *)malloc(sizeof(node));
                    root->lchild = NULL;
                    root->rchild = NULL;
                    root->data = -1;
                    flag=0;
                }
            }
        }
        return 0;
    }
    //节点未赋值  或者同一个节点赋值两次   就输出not complete 
  • 相关阅读:
    Unique Binary Search Trees——LeetCode
    Binary Tree Inorder Traversal ——LeetCode
    Maximum Product Subarray——LeetCode
    Remove Linked List Elements——LeetCode
    Maximum Subarray——LeetCode
    Validate Binary Search Tree——LeetCode
    Swap Nodes in Pairs——LeetCode
    Find Minimum in Rotated Sorted Array——LeetCode
    Linked List Cycle——LeetCode
    VR AR MR
  • 原文地址:https://www.cnblogs.com/20172674xi/p/10017750.html
Copyright © 2011-2022 走看看