文章

28

粉丝

230

获赞

23

访问

229.1k

头像
二叉树的创建方法值得关注
P1109 同济大学机试题
发布于2021年3月7日 21:40
阅读数 9.9k

#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...
登录查看完整内容


登录后发布评论

1 条评论
飞天老头
2023年4月4日 19:56

yes

赞(0)