创建二叉树的三种算法:1、结构体定义:structnode{structnode*lchild;structnode*rchild;charch;};2、算法部分1)递归创建二叉树(无返回值法):voidcreate(structnode**T){charch;std::cin>>ch;if(ch==‘#’)*T=NULL;else{*T=(structnode*)malloc(sizeof(structnode));*T->ch=ch;create(&(*T)->lchild);create(&(*T)->rchild);}}搭配上主函数即可使用:intmain(void){structnode*tree;create(&tree);return0;}2)递归创建二叉树(有返回值):structnode*create(structnode*T){charch;std::cin>>ch;if(ch!=‘#’){T=(structnode*)malloc(sizeof(structnode));T->ch=ch;T->lchild=create(T->lchild);T->rchild=create(T->rchild);returnT;}returnNULL;}搭配主函数即可使用:intmain(void){structnodeT,*head;head=create(&T);return0;}3)非递归创建树:structnode*create(){charch[20];inti=0,flag=0,top=0;structnode*tree,*head,*stack[20],*st;std::cin>>ch;tree=(structnode*)malloc(sizeof(structnode));head=tree;tree->data=ch[i];tree->lchild=NULL;tree->rchild=NULL;stack[top++]=tree;inta=0;i++;while(irchild==st){st=stack[--top];}}elseif((ch[i]=='#')&&(flag==0)){flag=1;}elseif(ch[i]!='#'){tree=(structnode*)malloc(sizeof(structnode));tree->data=ch[i];tree->lchild=NULL;tree->rchild=NULL;if(flag==0){stack[top-1]->lchild=tree;stack[top]=tree;top++;}if(flag==1){flag=0;stack[top-1]->rchild=tree;top++;}}else{std::cout<<"sorry,yourenteriserror"<