用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. ModelSim Simulation of RapidIO II IP Core Demonstration Testbench May Require ld_debug Command

    Solution ID: fb83262Last Modified: May 17, 2013Product Category: Intellectual PropertyProduct Area: ...

  2. [译]Javascript在ASP NET中的运用

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

  3. Oracle数据库网闸配置注意事项

    1.数据库用户需要的权限 grant select any dictionary to coss; grant alter any procedure to coss; grant create tr ...

  4. 关于 node-sass 安装失败的问题

    最近换了一台计算机,使用ionic+cordova 开发,过程中发现node-sass 总是安装失败,经过搜索之后,发现可能是由于国内被墙导致的,找到了解决办法记录一下: 使用taobao 的镜像,成 ...

  5. C#中的线程池使用(二)

    线程池是后台线程.每个线程都使用默认的堆栈大小,以默认的优先级运行,并处于多线程单元中.每个进程只有一个线程池对象. 下面说一下线程池中的异常,在线程池中未处理的异常将终止进程.以下为此规则的三种例外 ...

  6. Memcached Cache

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using Memcached ...

  7. ASP.NET网页动态添加数据行

    一看到这标题<ASP.NET网页动态添加数据行>,想起来似乎有点难实现.因为网页的周期性原因,往往在PostBack之后,状态难于有所保留.但Insus.NET又想实现这样的效果,用户点击 ...

  8. cenos云服务器搭建虚拟主机

    ---恢复内容开始--- vim基本操作 1.如果apache安装成为Linux的服务的话,可以用以下命令操作: service httpd start 启动 service httpd restar ...

  9. Thinkphp手把手练习

    一.搭建thinkphp开发环境 准备条件:thinkphp框架 1.在Apache的www目录下新建文件夹,命名为thinkphp,可以将THinkPHP框架放在该目录中. 2.在thinkphp目 ...

  10. kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence

    Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...