- #include <iostream>
- using namespace std;
- typedef struct node {
- int count;
- node *lchild;
- node *rchild;
- }*tree;
- int main(int argc, const char * argv[]) {
- int n,f = -1;
- cin >>n;
- node *t = (node*)malloc(sizeof(node));
- cin >>t->count;
- t->lchild = NULL;
- t->rchild = NULL;
- cout <<f<<"\\n";
- node *p,*k;
- int i=1;
- while (i<n) {
- k = (node*)malloc(sizeof(node));
- cin >>k->count;
- k->lchild = NULL;
- k->rchild = NULL;
- p = t;
- while (NULL != p) {
- f = p->count;
- if (k->count>p->count) {
- if (!p->rchild) {
- p->rchild = k;
- break;
- }
- p = p->rchild;
- }
- else {
- if (!p->lchild) {
- p->lchild = k;
- break;
- }
- p = p->lchild;
- }
- }
- cout <<f<<"\\n";
- i++;
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0807201614834.html
来源: http://www.codesnippet.cn/detail/0807201614834.html