题目1467:二叉排序树
注意是多组数据
#include <cstdio>
#include <cstdlib>
#include <cstring>
typedef struct node_ *node;
struct node_ {
int key;
node lc, rc;
};
int insert(node &t, int fk, int k) {
if (t) {
if (k < t->key)
return insert(t->lc, t->key, k);
else
return insert(t->rc, t->key, k);
} else {
t = (node) malloc(sizeof(node_));
t->key = k;
t->lc = t->rc = NULL;
return fk;
}
}
int main(int argc, char const *argv[])
{
int n;
while (~scanf("%d", &n)) {
int k;
node tree = NULL;
while (n--) {
scanf("%d", &k);
printf("%d\n", insert(tree, -1, k));
}
}
return 0;
}