标签:
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <algorithm>
#define ss(a) scanf("%d",&a)
#define ss64(a) scanf("%I64d",&a)
using namespace std;
typedef struct Node
{
struct Node *l;
struct Node *r;
int num;
}*node;
void creat(node &T,int num) //此处的&的引用是关键
{
if (T == NULL)
{
T = (node)malloc(sizeof(Node));
T->num = num;
T->l = NULL;
T->r = NULL;;
}
else
if (num < T->num)
{
creat(T->l,num);
}
else
{
creat(T->r,num);
}
}
void post_visit(node T,int x)
{
if (T != NULL)
{
if(x)
cout<<T->num;
else
cout<<" "<<T->num;
post_visit(T->l,0);
post_visit(T->r,0);
}
}
int main()
{
int i,n,m,sum,cnt,j;
int num;
node T=NULL;
while(~ss(n))
{
for (i=0;i<n;i++)
{
ss(num);
creat(T,num);
}
post_visit(T,1);
cout<<endl;
}
return 0;
}
考察二叉树的创建以及先序遍历
4 1 3 4 2
1 3 2 4
HDU 3999 The order of a Tree 二叉树
标签:
原文地址:http://blog.csdn.net/xinwen1995/article/details/45722299