122:Trees on the level

Trees on the level

 

哈哈 这次是一次AC,好开心啊
version 1(指针链表版):

#include<bits/stdc++.h>
using namespace std;
const int maxn = 256 + 5;
struct node{
    int num = -1;
    struct node* left = NULL;
    struct node* right = NULL;
};
int main(){
    // freopen("data.in","r",stdin);
    // freopen("data.out","w",stdout);
    char s[maxn];
    for(;;){
        int mark = 1;
        node* root = new node;
        while(scanf("%s",s) != EOF && s[1] != ')'){
            node* N = root;
            int num = atoi(s+1);
            int i = 0;
            while(s[i] != ',') i++;
            i++;
            while(s[i] != ')'){
                if(s[i] == 'L'){
                    if(!N->left){
                        node* t = new node;
                        N->left = t;
                    }
                    N = N->left;
                }
                else{
                    if(!N->right){
                        node* t = new node;
                        N->right = t;
                    }
                    N = N->right;
                }
                i++;
            }
            if(N->num == -1) N->num = num;
            else mark = 0;
        }
        if(!root->left && !root->right && root->num == -1) break;
        if(!mark){ puts("not complete"); continue; }
        else{
            queue<node*>qt;
            queue<int>qn;
            qt.push(root);
            while(qt.size()){
                node* t = qt.front(); qt.pop();
                if(t->num == -1){ mark = 0; break; }
                qn.push(t->num);
                if(t->left) qt.push(t->left);
                if(t->right) qt.push(t->right);
            }
            if(!mark) puts("not complete");
            else{
                while(qn.size()){
                    printf("%d",qn.front());
                    qn.pop();
                    if(qn.size()) putchar(' ');
                }
                putchar('\n');
            }
        }
    }
    return 0;
}
version 2(release memory):
#include<bits/stdc++.h>
using namespace std;
const int maxn = 256 + 5;
struct node{
    int num = -1;
    struct node* left = NULL;
    struct node* right = NULL;
};
void remove_tree(node* u){
    if(!u) return;
    remove_tree(u->left);
    remove_tree(u->right);
    delete u;
}
int main(){
    // freopen("data.in","r",stdin);
    // freopen("data.out","w",stdout);
    char s[maxn];
    for(;;){
        int mark = 1;
        node* root = new node;
        while(scanf("%s",s) != EOF && s[1] != ')'){
            node* N = root;
            int num = atoi(s+1);
            int i = 0;
            while(s[i] != ',') i++;
            i++;
            while(s[i] != ')'){
                if(s[i] == 'L'){
                    if(!N->left){
                        node* t = new node;
                        N->left = t;
                    }
                    N = N->left;
                }
                else{
                    if(!N->right){
                        node* t = new node;
                        N->right = t;
                    }
                    N = N->right;
                }
                i++;
            }
            if(N->num == -1) N->num = num;
            else mark = 0;
        }
        if(!root->left && !root->right && root->num == -1) break;
        if(!mark){ puts("not complete"); continue; }
        else{
            queue<node*>qt;
            queue<int>qn;
            qt.push(root);
            while(qt.size()){
                node* t = qt.front(); qt.pop();
                if(t->num == -1){ mark = 0; break; }
                qn.push(t->num);
                if(t->left) qt.push(t->left);
                if(t->right) qt.push(t->right);
            }
            if(!mark) puts("not complete");
            else{
                while(qn.size()){
                    printf("%d",qn.front());
                    qn.pop();
                    if(qn.size()) putchar(' ');
                }
                putchar('\n');
            }
        }
        remove_tree(root);
    }
    return 0;
}


 
posted @ 2018-05-28 23:41  ACLJW  阅读(126)  评论(0编辑  收藏  举报