C++计算二叉树的节点数和高度
用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++计算二叉树的节点数和高度的更多相关文章
- LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
- C++实现二叉树的基本操作:建立、遍历、计算深度、节点数、叶子数等
题意: 代码实现: #include<iostream> #include<queue> #include<stack> using namespace std; ...
- 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离
数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...
- Z-tree 统计每一父节点的叶子节点数(看这一篇就够了)
最近刚走出校园的我找到了第一份工作,在入职考核中就遇见了一道Z-tree的试题 这道题目本身是不难的,但是我第一次接触这个插件而且还把解决问题的方向搞错了,弄的我好几天都很难受. 弄得我都开始怀疑人生 ...
- Codevs 1501 二叉树的最大宽度和高度
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描 ...
- 六:二叉树中第k层节点个数与二叉树叶子节点个数
二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...
- elk集群配置配置文件中节点数配多少
配置elk集群时,遇到,elasticsearch配置文件中的一个配置discovery.zen.minimum_master_nodes: 2.这里是三配的2 看到某一位的解释是这样:为了避免脑裂, ...
- python计算文件的行数和读取某一行内容的实现方法
一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...
- js计算字符串的字节数和字符串与二进制的相互转化
一.js计算字符串的字节数方法: //blob获取字符串的字节 var debug = "好的"; var blob = new Blob([debug],{type : 'tex ...
随机推荐
- lua遍历文件
看了不少人的,主要还是错误处理有点问题,不多说了 贴代码: require "lfs" function getpathes(rootpath, pathes) pathes = ...
- android studio中配置X5 webview时的一个坑
在接入X5的第二步中,需要配置so文件,这里说一下遇到的坑 1. 需要把demo下载回来,不然你找不到so文件,找到so文件后放到自己相对应的目录下边,自己里面那个目录也没有,所以和文件夹一起复制过去 ...
- Vue 组件 生命周期函数mounted
生命周期函数mounted:页面刷新触发mounted(){ console.log('我在页面刷新时触发');} Tips:使用export default function Add(){},与ex ...
- Django之博客系统搭建一
前面已经介绍了django的各种用法,从这一章开始,将实际搭建一个blog系统. 首先我们需要设计blog的模型,在models.py中添加如下内容 # -*- coding: utf-8 -*- f ...
- 1、OpenCV Python 图像加载和保存
__author__ = "WSX" import cv2 as cv # 这里的文件是图片或者视频 def Save_File( image ): cv.imwrite(&quo ...
- socket中close发生的事情,RST,pipe信号错误
1.server端close之后,client端write,导致server端发送RST(服务器关闭套接字):对方已经关闭或者异常终止,但是client端,不知道,这个成为半打开 当server端cl ...
- zabbix监控java内存的脚本
#!/bin/bash # 截取java的pid号 java_pid=`netstat -lnpt |grep |awk -F '/' '{print $1'}` # 截取$jstat命令的位置 js ...
- git ssh密钥的使用
//配置邮箱,用户名, git config --global user.name git config --global user.email git config --global --list ...
- [NodeJS]exit code
NodeJs 如何返回exit Code code =1; process.emit("exit ") process.reallyExit(code);
- 项目中遇到的问题:前台 disabled 与 后台disabled
TPRI项目流程,点[保存],“人员”服务器端控件,如果在前台disabled设置,则值会丢失:在后台设置disabled就可以.