用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. 缓存淘汰算法之LRU

    1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. ...

  2. Http工作原理(转)

    HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议.它可以使浏览器更加高效,使网络传输减少.它不仅保证计算机正确 ...

  3. 记一次安装kolla遇到DockerException: Error while fetching server API version: Timeout value connect was Timeout的问题

    1)环境信息: docker版本:17.09,当docker的版本是12.06时,也会报这个错误 [root@localhost ~]# docker --version Docker version ...

  4. UIApplication直接应用

    /************ 当程序载入后执行,应用程序启动入口 *****************************/ - (BOOL)application:(UIApplication *) ...

  5. numpy中argsort函数用法

    在Python中使用help帮助 >>> import numpy >>> help(numpy.argsort) Help on function argsort ...

  6. 并查集【洛谷P1197】 [JSOI2008]星球大战

    P1197 [JSOI2008]星球大战 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系 ...

  7. 关于如何在Windows下测交互题

    这里的交互题指的NOI风格的交互题,即交互库 codeforces风格的交互题...只能自己实现评测插件了 使用Cena,Lemon没有附加文件功能不能评测交互题 在编译选项g++编译命令源文件中加入 ...

  8. 10.9 guz模拟题题解

    感谢@guz 顾z的题题解 考试共三道题,其中 第一题help共10个测试点,时间限制为 1000ms,空间限制为 256MB. 第二题escape共20个测试点,时间限制为1000ms2000ms, ...

  9. jdk 1.6.0_43 下载

    Java SE Development Kit 6u43 Product / File Description File Size Download password Linux x86 65.43 ...

  10. Warning: Call to 'toArray()' with pre-sized array argument 'new String[list.size()]'

    当使用如下代码将List转换为Array类型时: List<String> list = new ArrayList<>(); String[] array = list.to ...