http://acm.split.hdu.edu.cn/showproblem.php?pid=3999
建树+前缀表达式
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
struct node
{
int data;
node *left;
node *right;
}tree;
node* Insert(node *root,int x)
{
if(root==NULL)
{
root=new node;
root->data=x;
root->left=root->right=NULL;
return root;
}
if(root->data>x)
root->left=Insert(root->left,x);
else root->right=Insert(root->right,x);
return root;
}
void PreOrder(node *rt,bool flag)
{
if(flag)
printf("%d",rt->data);
else printf(" %d",rt->data);
if(rt->left)
PreOrder(rt->left,0);
if(rt->right)
PreOrder(rt->right,0);
}
int main()
{
int n;
node *root;
while(~scanf("%d",&n))
{
root=NULL;
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
root=Insert(root,x);
}
PreOrder(root,1);
puts("");
}
return 0;
}