标签:二叉树
1 2 2 1 20
2 1 20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int data;
struct node *l,*r;
};
int n;
int cnt;//控制中序输出格式;
struct node *creat(struct node *&root,int a)//记得是*&root
{
if(root==NULL)
{
root=(struct node *)malloc(sizeof(struct node));
root->l=NULL;
root->r=NULL;
root->data=a;
}
else
{
if(a<root->data)
creat(root->l,a);
else
creat(root->r,a);
}
}
void zhongxu(struct node *root)
{
if(root)
{
zhongxu(root->l);
if(cnt==1)
{
printf("%d",root->data);
cnt++;
}
else
printf(" %d",root->data);
zhongxu(root->r);
}
}
int main()
{
int i;
int x;
while(~scanf("%d",&n))
{
cnt=1;
struct node *root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
creat(root,x);
}
zhongxu(root);
printf("\n");
}
return 0;
}
标签:二叉树
原文地址:http://blog.csdn.net/u013486414/article/details/41135481