【C++】计算二叉树的深度(递归+非递归)

相较于非递归,递归算法更加简洁实用👇

	//递归算法求二叉树深度
	int CaculateTreeDepth1(BinaryTreeNode<T>* treeNode) {
		if (treeNode == NULL){
			return 0;
		}
		int depth = 0;
		int leftDepth = CaculateTreeDepth1(treeNode->getLeft());//求左子树的深度		
		int rightDepth = CaculateTreeDepth1(treeNode->getRight());//求右子树的深度
		depth = leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;//求二叉树的深度
		return depth;
	}

非递归算法用到了队列,通过利用队列先进先出的特点来计算深度👇

(懒得重新写一个队列了,就直接用queue头文件了)

#include<queue>

关于queue的使用

	//非递归算法求二叉树深度
	int CaculateTreeDepth2(BinaryTreeNode<T>* treeNode) {
		queue<BinaryTreeNode<T>*> q;
		if (treeNode == NULL) {
			return 0;
		}
		q.push(treeNode);
		int depth = 0;
		while (!q.empty()) {
			depth++;
			for (int size = q.size(); size > 0; size--) {
				BinaryTreeNode<T>* p = q.front();
				q.pop();
				if (p->getLeft()) {
					q.push(p->getLeft());
				}
				if (p->getRight()) {
					q.push(p->getRight());
				}
			}
		}
		return depth;
	}

最后附上完整代码👇

#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;
	}
	//访问结点
	void VisitNode() {
		cout << data << " ";
	}
	//取左结点
	BinaryTreeNode<T>* getLeft() {
		return leftchild;
	}
	//取右结点
	BinaryTreeNode<T>* getRight() {
		return rightchild;
	}
};

//二叉树类
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;
	}
	//递归算法求二叉树深度
	int CaculateTreeDepth1(BinaryTreeNode<T>* treeNode) {
		if (treeNode == NULL){
			return 0;
		}
		int depth = 0;
		int leftDepth = CaculateTreeDepth1(treeNode->getLeft());//求左子树的深度		
		int rightDepth = CaculateTreeDepth1(treeNode->getRight());//求右子树的深度
		depth = leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;//求二叉树的深度
		return depth;
	}
	//非递归算法求二叉树深度
	int CaculateTreeDepth2(BinaryTreeNode<T>* treeNode) {
		queue<BinaryTreeNode<T>*> q;
		if (treeNode == NULL) {
			return 0;
		}
		q.push(treeNode);
		int depth = 0;
		while (!q.empty()) {
			depth++;
			for (int size = q.size(); size > 0; size--) {
				BinaryTreeNode<T>* p = q.front();
				q.pop();
				if (p->getLeft()) {
					q.push(p->getLeft());
				}
				if (p->getRight()) {
					q.push(p->getRight());
				}
			}
		}
		return depth;
	}
};
int main() {
	//前序构建二叉树
	BinaryTree<char> test;
	//求二叉树深度
	cout << endl << "递归求得深度:";
	cout << test.CaculateTreeDepth1(test.getRoot());
	cout << endl << "非递归求得深度:";
	cout << test.CaculateTreeDepth2(test.getRoot());
}

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mitch311

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

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

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

打赏作者

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

抵扣说明:

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

余额充值