/*
二叉树的插入,前序遍历。
*/
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int n;
struct node
{
int key;
node *rson;
node *lson;
}*root;
int ok;
void InsertTree(node **rt,int key)
{
if(*rt==NULL)
{
*rt=(node*)malloc(sizeof(node));
(*rt)->key=key;
(*rt)->lson=NULL;
(*rt)->rson=NULL;
}
else
{
if(key<(*rt)->key)
InsertTree(&((*rt)->lson),key);
else
InsertTree(&((*rt)->rson),key);
}
}
void PrintTree(node *rt)
{
if(rt!=NULL)
{
if(ok)
{
cout<<rt->key;
ok=0;
}
else
{
cout<<" "<<rt->key;
}
PrintTree(rt->lson);
PrintTree(rt->rson);
free(rt);
}
}
int main()
{
while(cin>>n)
{
ok=1;
root=NULL;
for(int i=0;i<n;i++)
{
int key;
cin>>key;
InsertTree(&root,key);
}
PrintTree(root);
cout<<endl;
}
return 0;
}
HDU 3999 The order of a Tree
最新推荐文章于 2021-03-14 10:44:44 发布