#include<stdio.h>
#include <stdlib.h>
typedef struct TreeNode
{
char val;
struct TreeNode* left;
struct TreeNode* right;
}TreeNode;
TreeNode* maketree(char*arr,int*count)//在递归调用中需要共享count值,所以加*
//传递参数的地址而不是参数的值,通过使用地址传递,可以在函数内部修改 count 参数的值并在递归中正确更新数组位置,确保下一个递归步骤可以正确继续遍历数组。
{
if(arr[*count]=='#'||arr[*count]=='\0')
{
return NULL;
}
TreeNode* newnode = (TreeNode*)malloc(sizeof(TreeNode));
newnode->val = arr[(*count)++];
newnode->left = maketree(arr,count);
(*count)++;
newnode->right = maketree(arr,count);
return newnode;
}
void Inorder(TreeNode* root)
{
if(root==NULL)
{
return;
}
Inorder(root->left);
printf("%c ",root->val);
Inorder(root->right);
}
int main()
{
char arr[101];
scanf("%s",arr);
int count = 0;
TreeNode* tree = maketree(arr,&count);
Inorder(tree);
return 0;
}