满二叉树的节点查找 我自己写的

#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 k);
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 k)
{
	node* tmp = NULL;
	if(tree==NULL)
	return NULL;
	if(tree!=NULL)
	{
		if(tree->date == k)
		{
			printf("wo,找到了\n");
			return tree;
		}
		
		tmp = findtree(tree->left,k);
		if(tmp!=NULL)
			return tmp;
		tmp = findtree(tree->right,k);
		if(tmp!=NULL)
		return tmp;
		}
	
}

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