c
复制代码
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
struct TreeNode* left;
struct TreeNode* right;
char val;
} TNode;
//创建节点
TNode* CreateTree(char* a,int*pi) {
if(a[*pi] == '#')
{
++(*pi);
return NULL;
}
TNode* root = (TNode*)malloc(sizeof(TNode));
root->val = a[(*pi)++];
root->left = CreateTree(a,pi);
root->right = CreateTree(a,pi);
return root;
}
//前序遍历
void InOrder(TNode* root)
{
if(root == NULL)
return;
InOrder(root->left);
printf("%c ",root->val);
InOrder(root->right);
}
int main() {
char str[100];
scanf("%s", str);
int i = 0;
TNode* root = CreateTree(str,&i);
InOrder(root);
return 0;
}