一开始写的指针实现的版本,貌似存在内存泄漏问题,输入之间有蜜汁相互作用,后改为数组实现,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;
}