【C++】统计二叉树度为1的节点个数(递归+非递归)

首先我们要在结点类里加一个bool类型的函数用于判断该结点度是否为1👇

	//判断结点度是否为1
	bool isAlone() {
		if (leftchild == NULL && rightchild != NULL) {
			return true;
		}
		else if(leftchild != NULL && rightchild == NULL){
			return true;
		}
		else {
			return false;
		}
	}

随后我们再到二叉树类里编写我们的算法

 

递归算法和上一篇blog里的求二叉树深度(递归+非递归)非常类似👇

//递归统计二叉树度为1的节点个数
	int CaculateAlone1(BinaryTreeNode<T>* treeNode) {
		int count = 0;
		if (treeNode != NULL) {
			if (treeNode->isAlone()) {
				count++;
			}
			count += CaculateAlone1(treeNode->getLeft());
			count += CaculateAlone1(treeNode->getRight());
		}
		return count;
	}

非递归也和求深度差不多,核心也是借助队列先进先出的特点进行操作👇

//非递归统计二叉树度为1的节点个数
	int CaculateAlone2(BinaryTreeNode<T>* treeNode) {
		int count = 0;
		if (treeNode != NULL) {
			queue<BinaryTreeNode<T>*> q;
			q.push(treeNode);
			while (!q.empty()) {
				for (int size = q.size(); size > 0; size--) {
					BinaryTreeNode<T>* p = q.front();
					q.pop();
					if (p->isAlone()) {
						count++;
					}
					if (p->getLeft()) {
						q.push(p->getLeft());
					}
					if (p->getRight()) {
						q.push(p->getRight());
					}
				}
			}
		}
		return count;
	}

最后附上完整代码👇

#include<iostream>
#include<queue>
using namespace std;

//结点类
template<class T>
class BinaryTreeNode {
	//友元类声明
	template<class T>
	friend class BinaryTree;
private:
	T data;
	BinaryTreeNode<T>* leftchild;
	BinaryTreeNode<T>* rightchild;
public:
	//构造函数
	BinaryTreeNode() {
		leftchild = NULL;
		rightchild = NULL;
	}
	BinaryTreeNode(const T& dataValue) {
		data = dataValue;
		leftchild = NULL;
		rightchild = NULL;
	}
	BinaryTreeNode(const T& dataValue, BinaryTreeNode<T>* leftchildValue, BinaryTreeNode<T>* rightchildValue) {
		data = dataValue;
		leftchild = leftchildValue;
		rightchild = rightchildValue;
	}
	//析构函数
	~BinaryTreeNode() {
		leftchild = NULL;
		rightchild = NULL;
	}
	//取左结点
	BinaryTreeNode<T>* getLeft() {
		return leftchild;
	}
	//取右结点
	BinaryTreeNode<T>* getRight() {
		return rightchild;
	}
	//判断结点度是否为1
	bool isAlone() {
		if (leftchild == NULL && rightchild != NULL) {
			return true;
		}
		else if(leftchild != NULL && rightchild == NULL){
			return true;
		}
		else {
			return false;
		}
	}
};

//二叉树类
template<class T>
class BinaryTree {
private:
	BinaryTreeNode<T>* root;
public:
	//构造函数
	BinaryTree() {
		root = NULL;
		cout << "Now start to construct the BinaryTree" << endl;
		CreateNode(root);
	}
	//析构函数
	~BinaryTree() {
		root = NULL;
	}
	//前序构建二叉树
	void CreateNode(BinaryTreeNode<T>* &treeNode) {
		cout << "Please enter a value or '#' to stop:";
		T dataValue;
		cin >> dataValue;
		treeNode = new BinaryTreeNode<T>(dataValue);
		if (treeNode->data == '#') {
			delete treeNode;
			treeNode = NULL;
		}
		else {
			CreateNode(treeNode->leftchild);
			CreateNode(treeNode->rightchild);
		}
	}
	//取根节点
	BinaryTreeNode<T>* getRoot() {
		return root;
	}
	//递归统计二叉树度为1的节点个数
	int CaculateAlone1(BinaryTreeNode<T>* treeNode) {
		int count = 0;
		if (treeNode != NULL) {
			if (treeNode->isAlone()) {
				count++;
			}
			count += CaculateAlone1(treeNode->getLeft());
			count += CaculateAlone1(treeNode->getRight());
		}
		return count;
	}
	//非递归统计二叉树度为1的节点个数
	int CaculateAlone2(BinaryTreeNode<T>* treeNode) {
		int count = 0;
		if (treeNode != NULL) {
			queue<BinaryTreeNode<T>*> q;
			q.push(treeNode);
			while (!q.empty()) {
				for (int size = q.size(); size > 0; size--) {
					BinaryTreeNode<T>* p = q.front();
					q.pop();
					if (p->isAlone()) {
						count++;
					}
					if (p->getLeft()) {
						q.push(p->getLeft());
					}
					if (p->getRight()) {
						q.push(p->getRight());
					}
				}
			}
		}
		return count;
	}
};
int main() {
	//前序构建二叉树
	BinaryTree<char> test;
	//统计二叉树度为1的节点个数
	cout << endl << "递归求得度为1的节点个数:";
	cout << test.CaculateAlone1(test.getRoot());
	cout << endl << "非递归求得度为1的节点个数:";
	cout << test.CaculateAlone2(test.getRoot());
}

 

  • 12
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mitch311

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值