数据结构算法—递归算法求二叉树的叶子结点(C语言)

数据结构算法—递归算法求二叉树的叶子结点(C语言)

遍历过程采用先序序列。
在构造二叉树时,直接输入二叉树的先序序列,我在注释中有例子。

#include<stdio.h>
#include<malloc.h>
struct node{
	char info;
	struct node *llink,*rlink;
};
typedef struct node NODE;
NODE *creat(){
	char x;
	NODE *p;
    scanf("%c",&x);
	printf("%c",x);
	if(x!='.'){//测试输入 ABD..EH...CF.I..G..       . 表示该结点无子树  也就是返回上一层递归 
		p=(NODE *)malloc(sizeof(NODE));
		p->info=x;
		p->llink=creat();
		p->rlink=creat();
	 }
	else
		p=NULL;
		return p;
}
int Countleaf(NODE *bt,int count){//计算叶子结点 
	if(bt!=NULL){//根节点不为空 
		if(bt->llink==NULL&&bt->rlink==NULL)//左右子树都为NULL 即该结点为叶子结点,count ++  
            count++;
        count=Countleaf(bt->llink,count);// 不为空,先去遍历左子树,再次调用Countleaf函数。 
        count=Countleaf(bt->rlink,count);
	}
	return count;
}
int main(){
	NODE *T;
	int count = 0;
	printf("PLease input a tree:\n");
	T=creat();
	printf("\n");
	count = Countleaf(T,count);
	printf("递归算法-->二叉树的叶子结点数为:%d",count);
	printf("\n");
}
运行结果:
PLease input a tree:
ABD..EH...CF.I..G..
ABD..EH...CF.I..G..
递归算法—>二叉树的叶子结点数为:4

--------------------------------
Process exited after 15.37 seconds with return value 0

  • 13
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 题目要二叉树叶子节点的数量,使用C语言实现。 解题思路:采用递归遍历整棵二叉树,统计叶子节点的数量。若当前节点为空,则返回0;若当前节点没有左子和右子,则返回1;否则,将左子叶子节点数目和右子叶子节点数目加起来返回。 代码实现如下: ```c int countLeaf(TreeNode* root) { if(root == NULL) return 0; // 空节点返回0 if(root->left == NULL && root->right == NULL) return 1; // 叶子节点返回1 // 遍历左子和右子,将结果相加 return countLeaf(root->left) + countLeaf(root->right); } ``` ### 回答2: 递归算法是一种重要的计方法,它能够解决很多问题。在二叉树中,递归算法可以用来计叶子节点的个数。具体实现可以使用C语言的代码来进行描述。 为了计二叉树叶子结点数目,我们可以通过递归方式遍历整个二叉树。首先,需要定义一个函数来计叶子结点数目。这个函数需要判断当前节点是否为空,若为空,则返回0;如果当前节点的左右子节点都为空,则说明它是叶子节点,返回1;否则,递归遍历左右子,并返回左右子叶子节点数之和。 下面是用C语言实现递归算法二叉树叶子结点数目的例子代码: ``` #include<stdio.h> #include<stdlib.h> //定义二叉树结构体 struct node{ int data; struct node *left; struct node *right; }; //创建二叉树 struct node *create_tree(){ struct node *root; int val; scanf("%d",&val); if(val==-1){ root=NULL; }else{ root=(struct node*)malloc(sizeof(struct node)); root->data=val; root->left=create_tree(); root->right=create_tree(); } return root; } //计叶子结点数目的函数 int leaf_nodes(struct node *root){ if(root==NULL){ return 0; }else if(root->left==NULL && root->right==NULL){ return 1; }else{ return leaf_nodes(root->left)+leaf_nodes(root->right); } } int main() { struct node *root=create_tree(); int count=leaf_nodes(root); printf("The number of leaf nodes is %d\n",count); return 0; } ``` 上述代码中,`create_tree()`函数用于创建二叉树,`leaf_nodes()`函数用于计叶子节点个数,`main()`函数用于输入二叉树的数据,在屏幕中输出叶子节点个数。 ### 回答3: 递归算法在解决二叉树问题时非常常见,而计二叉树叶子节点数目也可以通过递归算法来实现。对于一棵二叉树来说,叶子节点就是没有子节点的节点,因此我们可以通过递归的方式,对每个节点进行判断,是否为叶子节点,如果是,则计数器加1,如果不是,则继续递归它的左右子。下面就是一个用C语言实现的递归算法二叉树叶子节点数目的示例代码: ``` #include<stdio.h> struct TreeNode{ int val; struct TreeNode *left; struct TreeNode *right; }; int leafNodesCount(struct TreeNode* root){ // 递归结束条件:节点为空 if(root == NULL) return 0; // 递归结束条件:节点为叶子节点 if(root->left == NULL && root->right == NULL) return 1; // 递归处理:左子和右子 int leftCount = leafNodesCount(root->left); int rightCount = leafNodesCount(root->right); // 返回结果:左右子叶子节点数目之和 return leftCount + rightCount; } int main(){ struct TreeNode root = {0}; struct TreeNode node1 = {1}; struct TreeNode node2 = {2}; struct TreeNode node3 = {3}; struct TreeNode node4 = {4}; struct TreeNode node5 = {5}; root.left = &node1; root.right = &node2; node1.left = &node3; node1.right = &node4; node2.right = &node5; int count = leafNodesCount(&root); printf("leaf nodes count is %d\n", count); return 0; } ``` 上面的代码实现了一个简单的二叉树,并通过leafNodesCount函数计了它的叶子节点数目,输出结果为3。该递归算法的时间复杂度为O(n),其中n为二叉树节点数目。可以看出,递归算法可以非常自然地处理二叉树问题,易于理解和实现,是二叉树问题解决的常用手段。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值