用struct结构体的写法:


/*
* description: 计算二叉树的层数和节点数
* writeby: nick
* date: 2012-10-23 16:16
*
*/ #include <iostream> using namespace std; struct node
{
int item;
node *l, *r;
node(int n)
{item=n; l=0; r=0;}
};
typedef node *link; //计算节点总数
int count(link h)
{
if(h==0) return 0;
return count(h->l) + count(h->r) + 1;
}
//计算叶子节点总数方法1
int leafcount(link h)
{
if(h==0) return 0;
if(h->l==null&&h->r==null) return 1;
return count(h->l) + count(h->r);
}
//计算叶子节点总数方法2
int leafcount(link h)
{
static int num=0;//static不建议这样写,最好放外边,这样写的话程序执行完了,这个变量还在内存中。
if(h==0) return 0;
if(h->l==null&&h->r==null) num++;
leafcount(h->l);
leafcount(h->r);
return num;
}

//计算高度 int height(link h) 
{ if(h==0) return -1;
int u=height(h->l);
int v=height(h->r);
return u>v?u+1:v+1; }
int main() {
link root = new node(4);
root -> l = new node(5);
root -> r = new node(6);
root->l->l = new node(7);
root->l->r = new node(8);
cout << count(root) << " " << height(root);
return 0; }

  

带class类的写法:

//叶子节点的个数
/*
(1)如果二叉树为空,返回0
(2)如果二叉树不为空且左右子树为空,返回1
(3)如果二叉树不为空,且左右子树不同时为空,返回左子树中叶子节点个数加上右子树中叶子节点个数
*/
[cpp] view plain copy print?
int GetLeafNodeNum(BTree* root)
{
if(root == NULL)
return 0;
if(root->m_pLeft == NULL && root->m_pRight == NULL)
return 1; int LeafNumOfLeft = GetLeafNodeNum(root->m_pLeft);
int LeafNumOfRight = GetLeafNodeNum(root->m_pRight); int ret = LeafNumOfLeft + LeafNumOfRight; return ret; } /*
判断量个二叉树的结构是否相同
1:如果两个二叉树都为空,那么返回true
2:如果一个二叉树为空,另外一个不为空,那么返回false
3:如果两个二叉树都不为空,那么如果它们的左子树和右子树的结果相同,返回true否则返回false
*/
[cpp] view plain copy print?
bool isEqual(BTree* root1, BTree* root2)
{
if(root1 == NULL && root2 == NULL)
return true;
else if ((root1 == NULL && root2!= NULL)|| (root1 != NULL && root2 == NULL))
return false; bool equalLeft = isEqual(root1->m_pLeft,root2->m_pLeft);
bool equalRight = isEqual(root1->m_pRight,root2->m_pRight); return (equalLeft && equalRight);
} 完整测试代码:
[cpp] view plain copy print?
// BTNumOfKLevel.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
using namespace std; class BTree
{
public:
int m_nValue;
BTree* m_pLeft;
BTree* m_pRight; BTree(int m):m_nValue(m)
{
m_pLeft = m_pRight = NULL;
} };
//二叉树的插入实现
void Insert(int value, BTree* &root)
{
if (root == NULL)
{
root = new BTree(value);
}
else if(value < root->m_nValue)
Insert(value,root->m_pLeft);
else if(value > root->m_nValue)
Insert(value,root->m_pRight);
else
;
} //叶子节点的个数
/*
(1)如果二叉树为空,返回0
(2)如果二叉树不为空且左右子树为空,返回1
(3)如果二叉树不为空,且左右子树不同时为空,返回左子树中叶子节点个数加上右子树中叶子节点个数
*/
int GetLeafNodeNum(BTree* root)
{
if(root == NULL)
return 0;
if(root->m_pLeft == NULL && root->m_pRight == NULL)
return 1; int LeafNumOfLeft = GetLeafNodeNum(root->m_pLeft);
int LeafNumOfRight = GetLeafNodeNum(root->m_pRight); int ret = LeafNumOfLeft + LeafNumOfRight; return ret; } /*
判断量个二叉树的结构是否相同
1:如果两个二叉树都为空,那么返回true
2:如果一个二叉树为空,另外一个不为空,那么返回false
3:如果两个二叉树都不为空,那么如果它们的左子树和右子树的结果相同,返回true否则返回false */ bool isEqual(BTree* root1, BTree* root2)
{
if(root1 == NULL && root2 == NULL)
return true;
else if ((root1 == NULL && root2!= NULL)|| (root1 != NULL && root2 == NULL))
return false; bool equalLeft = isEqual(root1->m_pLeft,root2->m_pLeft);
bool equalRight = isEqual(root1->m_pRight,root2->m_pRight); return (equalLeft && equalRight);
} int _tmain(int argc, _TCHAR* argv[])
{
BTree* m_pRoot = new BTree(4);
Insert(3,m_pRoot);
Insert(6,m_pRoot);
Insert(1,m_pRoot);
Insert(2,m_pRoot);
Insert(5,m_pRoot);
Insert(8,m_pRoot);
Insert(7,m_pRoot);
Insert(10,m_pRoot); BTree* m_pRoot2 = new BTree(4);
Insert(3,m_pRoot2);
Insert(6,m_pRoot2);
Insert(1,m_pRoot2);
Insert(2,m_pRoot2);
Insert(5,m_pRoot2);
Insert(8,m_pRoot2);
Insert(7,m_pRoot2);
Insert(10,m_pRoot2); int count = GetLeafNodeNum(m_pRoot);
cout<<"叶子节点的个数为:"<<count<<endl; cout<<"两个树的结构是否相同:"<<isEqual(m_pRoot,m_pRoot2); getchar();
return 0;
}

  

C++计算二叉树的节点数和高度的更多相关文章

  1. LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

  2. C++实现二叉树的基本操作:建立、遍历、计算深度、节点数、叶子数等

    题意: 代码实现: #include<iostream> #include<queue> #include<stack> using namespace std; ...

  3. 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离

    数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...

  4. Z-tree 统计每一父节点的叶子节点数(看这一篇就够了)

    最近刚走出校园的我找到了第一份工作,在入职考核中就遇见了一道Z-tree的试题 这道题目本身是不难的,但是我第一次接触这个插件而且还把解决问题的方向搞错了,弄的我好几天都很难受. 弄得我都开始怀疑人生 ...

  5. Codevs 1501 二叉树的最大宽度和高度

    1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描 ...

  6. 六:二叉树中第k层节点个数与二叉树叶子节点个数

    二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...

  7. elk集群配置配置文件中节点数配多少

    配置elk集群时,遇到,elasticsearch配置文件中的一个配置discovery.zen.minimum_master_nodes: 2.这里是三配的2 看到某一位的解释是这样:为了避免脑裂, ...

  8. python计算文件的行数和读取某一行内容的实现方法

    一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...

  9. js计算字符串的字节数和字符串与二进制的相互转化

    一.js计算字符串的字节数方法: //blob获取字符串的字节 var debug = "好的"; var blob = new Blob([debug],{type : 'tex ...

随机推荐

  1. [译]Javascript 参数(arguments)对象

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

  2. json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式

     JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...

  3. .Net Core Api 使用版本控制

    1,安装Microsoft.AspNetCore.Mvc.Versioning NET Core Mvc中,微软官方提供了一个可用的Api版本控制库Microsoft.AspNetCore.Mvc.V ...

  4. Data Base 常用数据库参数的前缀表示符合

    可能参数化SQL语句不同,例如在Access中参数化SQL语句是在参数直接以“?”作为参数名,在SQL Server中是参数有“@”前缀,在MySQL中是参数有“?”前缀,在Oracle中参数以“:” ...

  5. 洛谷 P2141 珠心算测验

    嗯... 先看一下这个题.... 题目描述 珠心算是一种通过在脑中模拟算盘变化来完成快速运算的一种计算技术.珠心算训练,既能够开发智力,又能够为日常生活带来很多便利,因而在很多学校得到普及. 某学校的 ...

  6. Qt 学习之路 2(9):资源文件

    Qt 学习之路 2(9):资源文件  豆子  2012年8月31日  Qt 学习之路 2  62条评论 上一章节中我们介绍了如何使用QAction添加动作.其中,我们使用QIcon加载了一张 png ...

  7. 关于c++中const的基本用法

    c++中的const 有点类似于c里的宏定义#define,但是似乎是在宏定义基础上的代码优化,具体我解释不清,下面主要提到的是 const 在c++中的3中基本用法: 1.指向常量的指针 例如:co ...

  8. HDU1085 Holding Bin-Laden Captive!

    Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long t ...

  9. day15 面向对象 成员

    成员 1. 变量 1.实例变量 格式: 变量.xxx=xx (称为实例变量,也叫属性,字段)给对象用的 2.类变量 类变量:直接写在类中的变量就是类变量,类变量一般用类名来访问 其实就是类中相同的属性 ...

  10. python自动化day1

    一.变量 变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'cl ...