文章
28
粉丝
230
获赞
23
访问
244.0k
#include <stdio.h>
#include <stdlib.h>
typedef struct TNode
{
struct TNode * Lchild;
struct TNode * Rchild;
char Data;
}TNode;
TNode* createBinaryTree()
{
TNode *p;
char ch;
scanf(" %c",&ch);//注意这个细节:%c前面有一个空格
if(ch-'0'== 0) //如果到了叶子节点,接下来的左、右子树分别赋值为0
{
p = NULL;
}
else
{
p = (TNode*)malloc(sizeof(TNode));
p->Data = ch;
p->Lchild = createBinaryTree(); //递归创建左子树
p->Rchild = createBinaryTree(); //递归创建右子树
}
return p;
}
int main(int argc, char const *argv[])
{
TNode *root = NULL;
root = createBinaryTree();
pros(root);
printf("\n");
zhongs(root);
printf("\n");
hous(root);
printf("\n");
return 0;
}
void pros(TNode *tree){
if (tree!=NULL)
{
printf("%c ",tree->Data);
pros(tree->Lchild);
pros(tree->Rchild);
}
}
void zhongs(TNode *tree){
if (tree!=NULL)
{
zhongs(tree->Lchild);
p...
登录后发布评论