利用二级指针创建满二叉树

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int date;
	struct node * left;
	struct node * right;
}node;
int n;
void Creattree(struct node ** tree,int i);
void preorder_traverse(struct node *tree);
int main()
{
	struct node * b_tree=NULL;
	printf("请输入层数!\n");
	scanf("%d",&n);
	Creattree(&b_tree,1);
	preorder_traverse(b_tree);
	return 0;
}

void Creattree(node ** tree,int i)
{
	if(i==n)
	{
		(*tree)=(struct node *)malloc(sizeof(node));
		printf("已经到叶子了,请输入一个值\n");
		scanf("%d",&(*tree)->date);
		(*tree)->left=NULL;
		(*tree)->right=NULL;
	}
	else
	{
		printf("已经到了第%d个节点了,请输入数据\n",i);
		(*tree)=(struct node *)malloc(sizeof(node));
		scanf("%d",&((*tree)->date));
		Creattree(&(*tree)->left,i+1);
		Creattree(&(*tree)->right,i+1);
	}
}

void preorder_traverse(struct node * tree)
{
	if(tree != NULL)
	{
		printf("%d\n",tree->date);
		preorder_traverse(tree->left);
		preorder_traverse(tree->right);
	}
}

    原文作者:满二叉树
    原文地址: https://blog.csdn.net/qq_30331643/article/details/52744221
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞