#include<iostream>
#include<stdio.h>
using namespace std;
struct Node{
int key;
struct Node *lchild, *rchild;
};
Node* CreateNode(int a){
Node* temp = new Node;
temp->key = a;
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void In_Order(Node *root){
Node *temp = root;
if (temp == NULL) { return; }
else {
cout << temp->key << " ";
In_Order(temp->lchild);
In_Order(temp->rchild);
}
}
void CreateTree(Node* &root, int a){
Node* current = root;
Node *pre = NULL;
Node* temp = CreateNode(a);
while (current != NULL){
if (a < current->key){
pre = current;
current = pre->lchild;
}
else if(a > current -> key){
pre = current;
current = pre->rchild;
}
else { return; }
}
if (a < pre->key){
pre->lchild = temp;
}
else{
pre->rchild = temp;
}
}
int main(){
int temp;
scanf("%d", &temp);
char KongGe = ' ';
Node *root = CreateNode(temp);
for (; KongGe == ' ';){
scanf("%d", &temp);
KongGe = getchar();
CreateTree(root, temp);
}
In_Order(root);
system("pause");
return 0;
}
太难了 看不懂