#include <stdio.h> #include <stdlib.h> typedef struct Node { int val; struct Node* left; struct Node* right; } Node ; void insert(Node* head, int value) { if (value < head->val) { if (head->left == NULL) { Node* pNew = (Node*)malloc(sizeof(Node)); pNew->val = value; pNew->left = pNew->right = NULL; head->left = pNew; //插入节点后,将父节点值输出 printf("%d\n",head->val); } else { insert(head->left, value) ; } } else if (value > head->val) { if (head->right == NULL) { Node* pNew = (Node*)malloc(sizeof(Node)); pNew->val = value; pNew->left = pNew->right = NULL; head->right = pNew; printf("%d\n",head->val); } else { insert(head->right, value) ; } } } int main() { int n; while (scanf("%d ", &n) != EOF) { // 注意 while 处理多个 case // 64 位输出请用 printf("%lld") to int value; Node* head = (Node*)malloc(sizeof(Node)); scanf("%d", &head->val); printf("%d\n",-1); head->left = head->right = NULL; int cnt = 1; while (cnt < n) { scanf("%d", &value); insert(head, value); cnt++; } } return 0; }