중차적 반복과 층차적 복원 두 갈래 나무
1670 단어 시험 연구 재시험기 시험 연습
, , , ,
#include "stdio.h"
using namespace std;
struct Node
{
int data;
Node *leftN;
Node *rightN;
} node[32];
int cnt = 0;
Node *create()
{
node[cnt].leftN = NULL;
node[cnt].rightN = NULL;
int data = 0;
return &node[cnt++];
}
void preOrder(Node *n,int num)
{
if(num==0)
{
printf("%d",n->data);
}
else
{
printf(" %d",n->data);
}
if(n->leftN!=NULL)
{
preOrder(n->leftN,++num);
}
if(n->rightN!=NULL)
{
preOrder(n->rightN,++num);
}
}
void lasOrder(Node *n,int num)
{
if(n->leftN!=NULL)
{
lasOrder(n->leftN,++num);
}
if(n->rightN!=NULL)
{
lasOrder(n->rightN,++num);
}
if(num>=cnt-1)
{
printf("%d",n->data);
}
else
{
printf("%d ",n->data);
}
}
Node *insert(int l[],int n,int m[],int p,int lum)
{
Node *t = create();
// ,
int midI = 0;
for(int i = p;i<=lum;i++)
{
if(m[i]==l[0])
{
midI = i;
break;
}
}
t->data = m[midI];
if(p==lum)
{
return t;
}
//
int lp=-1,ll=-1,rp=-1,rl=-1;
if(pleftN = insert(at,index,m,lp,ll);
}
if(midIrightN = insert(at,index,m,rp,rl);
}
return t;
}
int main()
{
int n = 0;
scanf("%d",&n);
int l[32] = {0};
int m[32] = {0};
for(int i = 0;i