数据结构树二叉树计算节点_查找二叉树中叶节点的数量 数据结构

数据结构树二叉树计算节点

Algorithm:

算法:

One of the popular traversal techniques to solve this kind of problems is level order tree traversal (Read: Level Order Traversal on a Binary Tree) where we use the concept of BFS.

解决此类问题的一种流行的遍历技术是级别顺序树遍历(阅读: 二叉树上的级别顺序遍历 ),我们使用BFS的概念。

The basic idea to solve the problem is:

解决问题的基本思路是:

  1. Do a level order traversal and check whether the processed node has its left and right child both NULL.

    进行级别顺序遍历,并检查已处理节点的左,右子节点是否均为NULL 。

  2. If the processed node has its left and right child both NULL, it’s a leaf node.

    如果处理后的节点的左右子节点均为NULL,则为叶节点。

  3. Increase leaf node count

    增加叶节点数

Pseudocode:

伪代码:

struct BT{              // tree node type
int data;           //value
struct BT *left;    //pointer to left child
struct BT *right;   //pointer to right child
};
int noofleafnodes(struct BT *root){ // root of the tree
// BT refers to node of tree (datatype for the node);
struct BT *temp;                
struct Queue *q=Creat_queue();  //creating a queue
int count=0;
if(!root)   //root is null
return;
//**using level oder search**
EnQueue(q,root); // Enqueue the root in the queue
while(!emptyQueue(q)){
temp=DeQueue(q);  //Dequeue
// processing the node and checking whether both child nodes are NULL or not 
if(!temp->left && !temp->right) 
//increase no of leaves count if both child nodes are NULL (ensures it's a leaf  node)
count++;           
else{
if(temp->left)
EnQueue(q,temp->left); // if left child exists EnQueue
if(temp->right)
EnQueue(q,temp->right); // if right child exists EnQueue
}
}
DeleteQueue(q);
return count; // return no of leaf nodes
}

leaf nodes in a binary tree

Image source: wikipedia

图片来源:Wikipedia

Example:

例:

The leaf nodes in the above tree is 2, 5, 11, 4

上面树中的叶节点是2、5、11、4

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
class tree{    // tree node is defined
public:
int data;
tree *left;
tree *right;
};
int noofleafnodes( tree *root){
queue<tree*> q;  // using stl
tree* temp;
int count=0;
q.push(root);
while(!q.empty()){
temp=q.front();
q.pop();
//process node and check wheather leaf node or not
if(!temp->left && !temp->right){            
count++;
cout<<temp->data<<" ";
}
if(temp->left)
q.push(temp->left); //EnQueue
if(temp->right)
q.push(temp->right); //EnQueue
}
cout<<endl;
return count;
}
tree* newnode(int data)  // creating new node
{ 
tree* node = (tree*)malloc(sizeof(tree)); 
node->data = data; 
node->left = NULL; 
node->right = NULL; 
return(node); 
} 
int main() 
{ 
//**same tree is builted as shown in example**
int c;
tree *root=newnode(2); 
root->left= newnode(7); 
root->right= newnode(5); 
root->right->right=newnode(9);
root->right->right->left=newnode(4);
root->left->left=newnode(2); 
root->left->right=newnode(6);
root->left->right->left=newnode(5);
root->left->right->right=newnode(11);
cout<<"printing the leaf nodes......"<<endl; 
c=noofleafnodes(root);
cout<<"no of leaf nodes is : "<<c<<endl;	
return 0; 
} 

Output

输出量

printing the leaf nodes......
2 5 11 4
no of leaf nodes is : 4

翻译自: https://www.includehelp.com/data-structure-tutorial/find-the-number-of-leaf-nodes-in-a-binary-tree.aspx

数据结构树二叉树计算节点

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.tpcf.cn/news/546059.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

重磅!阿里推出国产开源JDK!

简介Alibaba Dragonwell 是一款免费的, 生产就绪型Open JDK 发行版&#xff0c;提供长期支持&#xff0c;包括性能增强和安全修复。阿里巴巴拥有最丰富的Java应用场景&#xff0c;覆盖电商&#xff0c;金融&#xff0c;物流等众多领域&#xff0c;世界上最大的Java用户之一。Al…

部分排序算法c语言实现

代码比较粗糙&#xff0c;主要是用于对排序算法的理解&#xff0c;因而忽略了边界和容错处理相关代码。 相关文档&#xff1a; Insert Sort ,Bubble Sort ,Select Sort ,Shell sort ,Quick sort ,Heap sort ,Merge sort on Wikipedia algorithm Repository :C语言实现部分排…

计算机等级考试二级ACCESS考试大纲

公共基础知识部分30分 专业语言部分 70分 基本要求 1. 具有数据库系统的基础知识。 2. 基本了解面各对象的概念。 3. 掌握关系数据库的基本原理。 4. 掌握数据库程序设计方法。 5. 能使用Access建立一个小型数据库应用系统。 考试内容 一、 数据库基础知识 1. 基本概念&#xf…

C语言 常用单词

main 主要的 printf(print format)格式输出 include , return ,if ,else ,switch ,case 机箱&#xff1b;案例&#xff1b; default 默认 ,for while break 暂停&#xff1b;间断&#xff1b; continue math int char 字符&#xff1b; …

ipv6寻址_有类和无类寻址:IPV4寻址| 计算机网络

ipv6寻址1)分类寻址 (1) Classful Addressing) IPv4 addressing used the concept of classes. This architecture is known as classful addressing. IPv4寻址使用类的概念。 这种体系结构称为类寻址 。 In the classful addressing, there are 5 classes in which the addre…

windows没有软盘 怎么解决

1这种情况我遇到过。 现象为&#xff1a;启动快到桌面之前&#xff0c;会出现红叉错误提示框&#xff0c;标题为“Windows-没有软盘”&#xff0c;内容为“驱动器中没有软盘。请在\Device\Harddisk1\DR5 中插入软盘”&#xff0c;有“取消”、“重试”、“继续”三个按钮。点“…

forth编程语言

forth 是一种基于栈的程序设计语言&#xff0c;其语法使用逆波兰表达式&#xff08;即后缀表达式&#xff09;&#xff0c;forth的黄金期是上世纪80年代&#xff0c;现在使用的人很少&#xff0c;但是却非常的有趣。还有一个以forth为基础开发的语言factor ,它增加了许多当代的…

安装TPCC-MySQL报错

2019独角兽企业重金招聘Python工程师标准>>> 安装TPCC-MySQL做压力测试&#xff0c;由于TPCC-MySQL是bzr工具进行版本控制的&#xff0c;所以要先安装bzr [rootmha_backup /root] #rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.r…

enumerate_Java Thread类的static int enumerate(Thread [] th)方法与示例

enumerate线程类static int枚举(Thread [] th) (Thread Class static int enumerate(Thread[] th)) This method is available in package java.lang.Thread.enumerate(Thread[] th). 软件包java.lang.Thread.enumerate(Thread [] th)中提供了此方法。 This method is used to …

自由职业的前半年,我是如何度过的?

生活中所受的苦&#xff0c;终会以一种形式回归。我是一个后知后觉的人&#xff0c;从 2009 年毕业到现在&#xff0c;已经有 11 年的光景了&#xff0c;参加工作的前几年我从没想过要快速的提升自己的技能&#xff0c;对待工作也没有全力以赴&#xff0c;这样的迷茫和随大流的…

Microsoft Visual C++ Runtime Library 错误解决办法

今天安装软件时&#xff0c;出现“Microsoft Visual C Runtime Library”错误&#xff0c;网上查了下解决方法&#xff0c;得以解决。现在分享下&#xff0c;给碰到相同问题的朋友。微软官方解释如下&#xff1a;症状在 Microsoft Windows XP 中运行自定义 Microsoft Visual C …

Eucalyptus常用查询命令

前言&#xff1a; Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems &#xff08;Eucalyptus&#xff09; 是一种开源的软件基础结构&#xff0c;用来通过计算集群或工作站群实现弹性的、实用的云计算。它最初是美国加利福尼亚大学 Santa …

the blocks problem(uva 101 or poj 1208)

题目描述见&#xff1a;uva 101 or poj 1208 关键在于彻底理解题目中搬积木的几个命令的含义&#xff0c;见具体分析 如果还不能理解题意&#xff0c;那么找一个正确通过的代码&#xff0c;编译并输入测试数据&#xff0c;查看其每一个命令的执行情况。如我的代码中162行注…

调整灰度图像的大小,而无需在Python中使用任何内置函数

In this program, we will be using two functions of OpenCV-python (cv2) module. Lets see their syntax and descriptions first. 在此程序中&#xff0c;我们将使用OpenCV-python(cv2)模块的两个功能。 首先让我们看看它们的语法和说明。 1) imread():It takes an absolu…

第一章 认识计算机

*(%)^*&!*第一讲 了解计算机基础知识一、计算机的发展历程1、计算机的起源&#xff08;1&#xff09;世界上第一台计算机&#xff1a;1946年诞生&#xff0c;名称为ENIAC。&#xff08;2&#xff09;世界上第一台并行计算机&#xff1a;1950年诞生&#xff0c;名称为EDVAC&…

scoket多线程例子

大体思路&#xff0c;有n台mc&#xff0c;要dump出数据&#xff0c;n台进行对比&#xff0c;看数据是否一致&#xff0c;设计到同时dump的问题&#xff0c;server断发条指令给这n台mc&#xff0c;mc同时去dump把结果返回给server端&#xff0c;server端把这些结果进行对比serve…

csapp bufbomb实验

csapp (《深入理解计算机系统》&#xff09;一书中有一个关于缓冲区溢出的实验&#xff0c;其程序代码如下&#xff1a; /* Bomb program that is solved using a buffer overflow attack */#include <stdio.h> #include <stdlib.h> #include <ctype.h> #in…

漫画:对象是如何被找到的?句柄 OR 直接指针?

小贴士&#xff1a;想要使用并定位 Java 对象&#xff0c;就要用到 Java 虚拟机栈&#xff08;Java Virtual Machine Stack&#xff09;&#xff0c;它描述的是 Java 方法执行的线程内存模型&#xff1a;每个方法被执行的时候&#xff0c;Java 虚拟机都会同步创建一个栈帧&…

在C ++中检查一个数组是否是另一个数组的子数组

Prerequisite: std::equal() function 先决条件&#xff1a; std :: equal()函数 Problem statement: 问题陈述&#xff1a; Check if one array is subarray of another or not. 检查一个数组是否是另一个数组的子数组。 Example: 例&#xff1a; Input 1:Arr1 [3, 4, 5, …

第二章 认识计算机硬件

*(%)^*&!*第一讲 认识计算机主板一、主板的结构1、主板结构分类&#xff08;2&#xff09;AT、Baby-AT型&#xff08;2&#xff09;ATX型&#xff08;3&#xff09;Micro ATX板型&#xff08;4&#xff09;LPX、NLX、Flex ATX板型&#xff08;5&#xff09;EATX、WATX板型&…