树结构练习——排序二叉树的中序遍历
Problem Description
Input
Output
Example Input
1 2 2 1 20
Example Output
2 1 20
#include <bits/stdc++.h>
using namespace std;
int l;
int a[10086];
typedef struct node
{
int data;
struct node *lc,*rc;
}tree;
tree *creat(tree *root,int k)
{
if(root==NULL)
{
root=new tree;
root->data = k;
root->lc = root->rc = NULL;
}
else
{
if(root->data>k)
{
root->lc = creat(root->lc,k);
}
else{
root->rc = creat(root->rc,k);
}
}
return root;
}
void inordertraval(tree *root)
{
if(root)
{
inordertraval(root->lc);
//printf("%c",root->data);
a[l++] = root->data;
inordertraval(root->rc);
}
}
int main()
{
int i,n,k;
struct node *root;
root = new tree;
while(scanf("%d",&n)!=EOF)
{
l=0;
root = NULL;
for(i=0;i<n;i++)
{
scanf("%d",&k);
root=creat(root,k);
}
inordertraval(root);
for(i=0;i<l;i++)
{
printf(i==0?"%d":" %d",a[i]);
}
printf("\n");
}
return 0;
}