广度ä¼å
éåæ¯åç»éåå§â¦â¦
åäºä¸ªå°ç¨åºï¼ä½ çç
#include<iostream>
using namespace std;
#define n 8
typedef struct node
{
char data;
struct node * left;
struct node * right;
}Node;
char post[n]={'g','d','b','e','h','f','c','a'};
char ino[n]={'d','g','b','a','e','c','h','f'};
Node *findchild(char *a,int i1,int j1,char *b,int i2,int j2)
{
if(i1>j1||i2>j2) return NULL;
Node *p=new Node;
int i;
p->data=a[j1];
for(i=i2;i<=j2;i++)
{
if(b[i]==a[j1])break;
}
p->left=findchild(a,i1,i1+i-i2-1,b,i2,i-1);
p->right=findchild(a,i1+i-i2,j1-1,b,i+1,j2);
return p;
}
void preorder(Node *t)
{
if(t!=NULL)
{
cout<<t->data<<" ";
preorder(t->left);
preorder(t->right);
}
}
int main()
{
Node *head,*t;
head=findchild(post,0,n-1,ino,0,n-1);
t=head;
preorder(t);
cout<<endl;
system("pause");
return 0;
}
追é®å
è°¢å¦