实现满二叉树的节点查找 书上的,竟然还错了,我给他改了

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int date;
	struct node * left;
	struct node * right;
}node;

int n;

node * Creattree(struct node * tree,int i);
node * findtree(struct node * tree,int x);
int main()
{
	struct node * b_tree=NULL,*tp=NULL;
	printf("请输入层数!\n");
	scanf("%d",&n);
	b_tree=Creattree(b_tree,1);
	tp=findtree(b_tree,2);
	if(tp==NULL)
	printf("非常抱歉,没有找到\n");
	else
	printf("找到了%d\n",tp->date);
	return 0;
}

node * Creattree(struct node * tree,int i)  //我依然认为这种构造二叉树的思想需要时不时的来反思一下 
{
	if(tree == NULL){
		tree=(struct node *)malloc(sizeof(node));
		tree->left=NULL;
		tree->right=NULL;
	} 
	if(i==n){
		printf("到了叶子了,请输入一个数据\n");
		scanf("%d",&(tree->date));
		return tree;
	}
	else
	{
		printf("到了第%d个节点,请输入一个数据\n",i);
		scanf("%d",&(tree->date));
		tree->left=Creattree(tree->left,i+1);
		tree->right=Creattree(tree->right,i+1);
	}
	return tree;
}
node * findtree(struct node *tree,int x)
{
	node * tmp=NULL; 
	if(tree == NULL)
	return NULL;
	else if(x == tree->date)
	return tree;
	else
	{
		tmp=findtree(tree->left,x);
		if(tmp!=NULL)
		return tmp;
		tmp=findtree(tree->right,x);
		if(tmp!=NULL)
		return tmp;
	}
}

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