Trees on the level UVa122

一开始写的指针实现的版本,貌似存在内存泄漏问题,输入之间有蜜汁相互作用,后改为数组实现,AC。

指针版本:

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

char x[300], cmd[300];
int flag;

struct node{
    int data = 0;
    bool check = false;
    node* left = NULL;
    node* right = NULL;
};

node* newNode()
{
    return new node();
}

node* root = NULL;
queue<node*> que;
vector<int> out;

int addNode(int num)
{
    //printf("%d %s\n", num, cmd);
    //if (cmd[0] == ')')
    //    root -> data = num, root -> check = true;
    int i = 0;
    node* p = root;
    while (cmd[i] != ')')
    {
        if (cmd[i] == 'L')
        {
            if(p -> left == NULL)
                p -> left = newNode();
            p = p-> left;
        }
        else 
        {
            if (p -> right == NULL)
                p -> right = newNode();
            p = p -> right; 
        }
        i++;
    }
    p -> data = num;
    if (p -> check)
        return 0;
    p -> check = true;

    //printf("%d %s\n", num, cmd);

    return 1;
}

void remove(node* p)
{
    if (p == NULL)  return;
    remove(p -> left);
    remove(p -> right);
    delete p;
}

int read()
{
    remove(root);
    flag = 0;
    root = new node();
    int num;
    while (scanf("%s", x) != EOF)
    {
        if (x[1] == ')')
            return 1;
        sscanf(x, "(%d,%s)", &num, cmd);
        flag = addNode(num);
    }
    return 0;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while (read())
    {
        out.clear();
        if (flag == 0)
        {
            printf("not complete\n");
        }
        else
        {
            node* p = root;
            que.push(root);

            while (!que.empty())
            {
                p = que.front();
                que.pop();
                if (p -> check)
                {
                    out.push_back(p -> data);
                    //printf("%d\n", p -> data);
                }
                else
                {
                    printf("not complete\n");
                    flag = 0;
                    break;
                }
                if (p -> left != NULL)
                    que.push(p -> left);
                if (p -> right != NULL)
                    que.push(p -> right);
            }
            if (flag)
            {
                printf("%d", out[0]);
                for (int i = 1; i < out.size(); i++)
                    printf(" %d", out[i]);
                printf("\n");
            }
        }
    }
    return 0;
}

数组版本(AC)

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

int root = 1, flag = 0, content[300], check[300], left[300], right[300], cnt = 1;
char x[300], cmd[300];
vector<int> out;

int newNode()
{
    cnt++;
    left[cnt] = 0;
    right[cnt] = 0;
    return cnt;
}

int addNode(int num)
{
    int p = 1, i = 0;
    while (cmd[i] != ')')
    {
        //printf("%d\n", cmd[i]);
        if (cmd[i] == 'L')
        {
            if (left[p] == 0)
            {
                left[p] = newNode();
            }
            p = left[p];
        }
        else
        {
            if (right[p] == 0)
            {
                right[p] = newNode();
            }
            p = right[p];
        }
        i++;

        //printf("%d %d %d\n", p, left[p], right[p]);

    }
    if (check[p])
        return 1;
    content[p] = num;
    check[p] = 1;
    return 0;
}

int read()
{
    memset(content, 0, sizeof content);
    memset(left, 0, sizeof left);
    memset(right, 0, sizeof right);
    memset(check, 0, sizeof check);
    root = 1;
    cnt = 1;
    flag = 0;               //flag为1,表示错误
    int num;
    while (scanf("%s", x) != EOF)
    {
        if (x[1] == ')')
            return 1;
        sscanf(x, "(%d,%s)", &num, cmd);
        //printf("%d %s", num, cmd);
        if(addNode(num))
            flag = 1;
    }
    return 0;
}
void bfs()
{
    out.clear();
    queue<int> que;
    int p = 1;
    que.push(p);
    while (!que.empty())
    {
        p = que.front();
        que.pop();

        //printf("%d %d %d %d \n", content[p], check[p], left[p], right[p]);

        if(check[p])
        {
            out.push_back(content[p]);
        }
        else
        {
            flag = 1;
            return;
        }
        if (left[p])
            que.push(left[p]);
        if (right[p])
            que.push(right[p]);
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while (read())
    {
        bfs();
        if (flag)
            printf("not complete");
        else
        {
            printf("%d", out[0]);
            for (int i = 1; i < out.size(); i++)
            {
                printf(" %d", out[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值