当前位置:网站首页>6-45 calculating the number of leaves of a binary tree
6-45 calculating the number of leaves of a binary tree
2022-06-22 09:35:00 【Xia ~ Chen】
This problem is to calculate the number of leaf nodes in a binary tree .
Function interface definition :
The function interface is described here . for example :
int num (Bptr p);Sample referee test procedure :
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *Lson,*Rson;
}Bnode,*Bptr;
int num (Bptr p);
Bptr creat()
{
Bptr p;
int x;
scanf("%d",&x);
if(x==0)
return NULL;
p=(Bptr)malloc(sizeof(Bnode));
p->data=x;
p->Lson=creat();
p->Rson=creat();
return p;
}
int main()
{
Bptr root;
int k;
root=creat();
k=num(root);
printf("%d\n", k);
return 0;
}
/* Please fill in the answer here */sample input :
3 4 1 0 0 0 2 0 0sample output :
2 Ideas : This problem requires us to calculate the number of leaves of a binary tree , that How to calculate ? First, we need to traverse the binary tree , Then look for the leaves of the binary tree . that , How do we judge whether it is a leaf node ? According to the definition of leaf node , As long as the left and right sons of the node are empty, we can judge whether the node is a leaf ! good , The conditions are , The number of leaves ? Here I have two ideas , One is that we set a count variable , For example count=0, Every time we traverse to a leaf node ,count Just +1; The other is , Suppose the traversal function is fun(), Then we go back , For example, matching to a node , We remember that 1, And then through fun(p->Lson)+fun(p->Rson) In this way, the number of leaf nodes is recursively returned , In this way, the final result is the number of leaf nodes , Don't talk much , Code up :
The first way :
int num(Bptr p) {
int count = 0;// Set calculation variables
if (p->Lson==NULL && p->Rson==NULL ) // Judge whether the current node is a leaf node
return 1;
if (p->Lson) // Determine whether the left son of the node exists
count+= num(p->Lson);
if(p->Rson) // Determine whether the right son of the node exists
count+= num(p->Rson);
return count;// Returns the number of leaf nodes
}The second way :
int num(Bptr p) {
if (p == NULL)// Judge whether the root node is empty
{
return 0;// Notice that this is 0 instead of 1 Oh
}
if (p->Lson == NULL && p->Rson == NULL)// Judge whether it is a leaf node
{
return 1;
}
return num(p->Lson)+num(p->Rson);// Returns the number of leaf nodes
}remind : In the second way, I will explain why I am p==NULL When to return to 0 instead of 1 Well ? Here we can think so , Suppose we have only one root node , So are its left and right sons empty , At this time it satisfies the first regulation , But it also meets the second condition , So if our first condition returns 1 Words , Whether the number of leaf nodes output here is 2 了 ? To avoid repetition , So we can only return to this place 0
边栏推荐
- PAT甲级 - 1013 Battle Over Cities(删点判连通块数量)
- Debian10安装Zabbix5.4
- Common SQL statements in MySQL
- mknod
- [hdu] P7079 Pty loves lines
- Redis 切片集群的数据倾斜分析
- Mapping Multi - export Server on ENSP
- C language question brushing | input a number and output the corresponding value (13)
- 求余弦的大小
- copy_ from_ User and copy_ to_ user
猜你喜欢

Machine learning | nltk_ Data download error |nltk's stopwords corpus download error solution

C# 进程如何使用非静态方法

SQL编程task04作业-集合运算

论文笔记:DETR: End-to-End Object Detection with Transformers (from 李沐老师and朱老师)

值(址)传递,看清名字,别掉沟里

Realize multi-user isolated FTP in AD environment

Langchao state-owned assets cloud: state-owned assets as a guide to help state-owned enterprises use data to enrich their wisdom in the cloud

Binary String

一个老开源人的自述-如何干好开源这件事

秋招秘籍A
随机推荐
[ZOJ] P3228 Searching the String
How C processes use non static methods
Traifik ingress practice
day575: 分糖果
通过docker安装mysql(5.7+8.0)并配置主从复制(GTID+增强半同步)
值(址)传递,看清名字,别掉沟里
C语言刷题 | 输入一个数输出对应的值(13)
rewrite? Reload? Are you dizzy?
Lexical Sign Sequence
Debian10 创建用户、用户组、切换用户
The difference between single bracket and double bracket in shell
Threejs implementation of simple panoramic view demo
模糊查询和聚合函数
. A use of file link library
秋招秘籍B
Try/finally --return those things
copy_ from_ User and copy_ to_ user
==经典面试题
C language question brushing | three item operation to realize capital judgment (16)
Logistic regression and linear regression