#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct node
{
int data, num;
struct node *l, *r;
} * q[100];
struct node *root = NULL;
struct node *buildtree(struct node *root, int x)
{
if (root == NULL)
{
root = (struct node *)malloc(sizeof(struct node));
root->data = x;
root->l = NULL;
root->r = NULL;
}
else if (x > root->data)
root->l = buildtree(root->l, x);
else
root->r = buildtree(root->r, x);
return root;
}
int main()
{
int n;
scanf("%d", &n);
int i;
for (i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
root = buildtree(root, x);
}
int flag = 1;
int a[100], cnt = 0;
int head = 0, tail = 0;
if (root != NULL)
{
q[tail] = root;
q[tail++]->num = 1;
while (head < tail)
{
struct node *t = q[head];
if (t->num > n)
flag = 0;
if (t->l)
{
t -> l -> num = t -> num * 2;
q[tail++] = t->l;
}
if (t->r)
{
t -> r -> num = t -> num * 2 + 1;
q[tail++] = t->r;
}
a[cnt++] = q[head++]->data;
}
}
for (i = 0; i < cnt; i++)
printf("%d%c", a[i], i == cnt - 1 ? '\n' : ' ');
if (flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
mmp
自己调试了一天的时间(maybe)终于给弄出来了,总结一下自己出现的错误。
1,补题的编译器只有gcc,所以用c写了代码,但是c的太多东西不一样,比如malloc忘记了,不能用node root之类的,应该用struct node root,还有,c没有stl库,所以队列不能用,必须自己创建。
2.most importantly !!! 自己将队列的队列的num值改变了,但是,二叉树没改变,到最后,都不明白哪里错了。。。。。。
3两个if语句中的tail是会改变的所以根节点也会变!!!!!!!
参考:https://blog.csdn.net/LMengi000/article/details/79616579?utm_source=blogxgwz6