题目连接:点击打开链接
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; struct node { int data; node *l,*r; }; int n; void Insert(node *&t,int data) { if(t==NULL) { t = new node; t->l = t->r = NULL; t->data = data; } else { if(data < t->data) Insert( t->l , data); else Insert( t->r , data); } } node *root; void Creat() { for(int i=0; i<n; i++) { int x; scanf("%d",&x); Insert(root,x); } } int stk[1001],l; void mid(struct node *T) { if(T!=NULL) { mid(T->l); stk[l++] = T->data; mid(T->r); } } void Delete(struct node *t) { if(t!=NULL) { Delete(t->l); Delete(t->r); } delete(t); } int main() { while(scanf("%d",&n)!=EOF) { l = 0; root = NULL; Creat(); mid(root); Delete(root); for(int i = 0;i<l-1;i++) { printf("%d ",stk[i]); } printf("%d\n",stk[l-1]); } return 0; }
原文:http://blog.csdn.net/wjw0130/article/details/39214373