#include <stdio.h>
#include <stdlib.h>
struct List {
struct List *next;
void *object;
} ;
typedef struct __queue {
struct List *front;
struct List *back;
} *Queue;
#define init(queue) do{\
queue = malloc(sizeof(struct __queue));\
if (queue == NULL) {\
perror("initial "#queue" error: ");\
exit(EXIT_FAILURE);\
}\
queue->back = queue->front = NULL;\
}while (0)
#define empty(queue) (queue->front == NULL)
#define front(queue) (queue->front->object)
void pop(Queue q) {
void *ans;
struct List *tmp;
if (empty(q))return;
ans = q->front->object;
tmp = q->front;
q->front = tmp->next;
}
void push(Queue q,void* ob) {
struct List *tmp;
tmp
= malloc(sizeof(struct List
)); if (tmp == NULL) {
perror("push object error: "); return;
}
tmp->next = NULL;
tmp->object = ob;
if (!empty(q))
q->back->next = tmp;
else
q->front = tmp;
q->back = tmp;
}
typedef struct __tree{
struct __tree *left;
struct __tree *right;
int num;
} *Tree;
void dfs_print(Tree t) {
if (t->left != NULL)
dfs_print(t->left);
if (t->right != NULL)
dfs_print(t->right);
}
int main(void) {
int num,flag_r,flag_l;
Queue qu;
Tree T = NULL,*tmp;
init(qu);
push(qu,&T);
while (!empty(qu)) {
tmp = front(qu);
pop(qu);
*tmp
= malloc(sizeof(struct __tree
)); if (*tmp == NULL) {
perror("add new node error: "); continue;
}
scanf("%d%d%d",&num
,&flag_l
,&flag_r
);
(*tmp)->left = NULL;
if (flag_l)
push(qu,&(*tmp)->left);
(*tmp)->right = NULL;
if (flag_r)
push(qu,&(*tmp)->right);
(*tmp)->num = num;
}
dfs_print(T);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCgoKc3RydWN0IExpc3QgewogICAgc3RydWN0IExpc3QgKm5leHQ7CiAgICB2b2lkICpvYmplY3Q7CiAgICB9IDsKCgp0eXBlZGVmIHN0cnVjdCBfX3F1ZXVlIHsKICAgIHN0cnVjdCBMaXN0ICpmcm9udDsKICAgIHN0cnVjdCBMaXN0ICpiYWNrOwogICAgfSAqUXVldWU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gdG1wOwoKICAgIHEtPmJhY2sgPSB0bXA7CiAgICB9CgoKdHlwZWRlZiBzdHJ1Y3QgX190cmVlewogICAgc3RydWN0IF9fdHJlZSAqbGVmdDsKICAgIHN0cnVjdCBfX3RyZWUgKnJpZ2h0OwogICAgaW50IG51bTsKICAgIH0gKlRyZWU7Cgp2b2lkIGRmc19wcmludChUcmVlIHQpIHsKICAgIHByaW50ZigiJWQgIix0LT5udW0pOwoKICAgIGlmICh0LT5sZWZ0ICE9IE5VTEwpCiAgICAgICAgZGZzX3ByaW50KHQtPmxlZnQpOwogICAgCiAgICBpZiAodC0+cmlnaHQgIT0gTlVMTCkKICAgICAgICBkZnNfcHJpbnQodC0+cmlnaHQpOwogICAgfQoKaW50IG1haW4odm9pZCkgewogICAgaW50IG51bSxmbGFnX3IsZmxhZ19sOwogICAgUXVldWUgcXU7CiAgICBUcmVlIFQgPSBOVUxMLCp0bXA7CiAgICAKICAgIGluaXQocXUpOwogICAgcHVzaChxdSwmVCk7CiAgICAKICAgIHdoaWxlICghZW1wdHkocXUpKSB7CiAgICAgICAgdG1wID0gZnJvbnQocXUpOwogICAgICAgIHBvcChxdSk7CgogICAgICAgICp0bXAgPSBtYWxsb2Moc2l6ZW9mKHN0cnVjdCBfX3RyZWUpKTsKICAgICAgICBpZiAoKnRtcCA9PSBOVUxMKSB7CiAgICAgICAgICAgIHBlcnJvcigiYWRkIG5ldyBub2RlIGVycm9yOiAiKTsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAKICAgICAgICBzY2FuZigiJWQlZCVkIiwmbnVtLCZmbGFnX2wsJmZsYWdfcik7CiAgICAgICAgCiAgICAgICAgKCp0bXApLT5sZWZ0ID0gTlVMTDsKICAgICAgICBpZiAoZmxhZ19sKQogICAgICAgICAgICBwdXNoKHF1LCYoKnRtcCktPmxlZnQpOwoKICAgICAgICAoKnRtcCktPnJpZ2h0ID0gTlVMTDsKICAgICAgICBpZiAoZmxhZ19yKQogICAgICAgICAgICBwdXNoKHF1LCYoKnRtcCktPnJpZ2h0KTsKCiAgICAgICAgKCp0bXApLT5udW0gPSBudW07CiAgICAgICAgfQogICAgZGZzX3ByaW50KFQpOwogICAgcHV0Y2hhcignXG4nKTsKICAgIHJldHVybiAwOwogICAgfQo=