感觉逻辑没问题,就是不知道哪里错了,各位帮帮忙,看了好几天了~~~
#include <iostream>
using namespace std;
#include <stdlib.h>
#define NULLVALUE 0
typedef int Elemtype;
typedef struct Tree{
Elemtype data;
struct Tree *LTree;
struct Tree *RTree;
};
void Create(Tree *tree,Elemtype *data,int &start,int len){
if(start>=len||data[start]==NULLVALUE){
return ;
}
else{
tree=(Tree *)malloc(sizeof(Tree));
tree->data=data[start];
++start;
Create(tree->LTree,data,start,len);
++start;
Create(tree->RTree,data,start,len);
}
}
void Print(Tree *tree){
if(tree==NULL)
return ;
cout<<tree->data<<" ";
Print(tree->LTree);
Print(tree->RTree);
}
int main(){
Elemtype data[]={1,2,4,0,0,5,0,0,3,6,0,0,7};
int len=sizeof(data)/sizeof(data[0]);
Tree *tree;
int start=0;
Create(tree,data,start,len);
Print(tree);
return 0;
}