#include<stdio.h>
#include<stdlib.h>
//定义树节点的结构
typedef struct Node
{
char data;
struct Node *left, *right;
}TNode, *TreeNode;
int i = -1;
//通过先序输入建立二叉树,然后通过中序遍历输出,使用二级指针
void InitTree(TreeNode *Tr,char* arr){
i++;
if(arr[i] != '\0'){
if(arr[i] != '#'){
//分配内存
if(((*Tr) = (TreeNode)malloc(sizeof(TNode))) == NULL){
exit(0);
}else{
(*Tr) ->data = arr[i];
(*Tr)->left = NULL;
(*Tr)->right = NULL;
InitTree(&((*Tr)->left),arr);
InitTree(&((*Tr)->right),arr);
}
}else{
(*Tr) = NULL;
}
}
}
//中序打印
void Middle_Search(TreeNode head){
if(head){
Middle_Search(head->left);
printf("%c",head ->data);
Middle_Search(head->right);
}
}
int main(){
char* arr = "5#6##";
TreeNode head= NULL;
InitTree(&head,arr);
Middle_Search(head);
printf("\n");
return 0;
}
结果: