求二叉树的深度和宽度

本文介绍了一种使用C++实现的算法,该算法能够有效地计算二叉树的深度和宽度。通过递归方式计算深度,利用队列遍历二叉树来计算每一层的节点数并找到最大宽度。
// 求二叉树的深度和宽度.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <queue>
using namespace std;

struct BTNode
{
	char m_value;
	BTNode *m_left;
	BTNode *m_right;
};

//先序创建二叉树
void CreatBTree(BTNode *&root)
{	
	char nValue = 0;
	cin >> nValue;
	if ('#' == nValue)
	{
		return;
	}
	else
	{
		root = new BTNode();
		root->m_value = nValue;
		CreatBTree(root->m_left);
		CreatBTree(root->m_right);
	}	
}

//求二叉树的深度
int GetDepth(BTNode *pRoot)
{
	if (pRoot == NULL)
	{
		return 0;
	}

	// 	int nLeftLength = GetDepth(pRoot->m_left);
	// 	int nRigthLength = GetDepth(pRoot->m_right);
	// 	return nLeftLength > nRigthLength ? (nLeftLength + 1) : (nRigthLength + 1);

	return GetDepth(pRoot->m_left) > GetDepth(pRoot->m_right) ? 
		(GetDepth(pRoot->m_left) + 1) : (GetDepth(pRoot->m_right) + 1);
}

//求二叉树的宽度
int GetWidth(BTNode *pRoot)
{
	if (pRoot == NULL)
	{
		return 0;
	}

	int nLastLevelWidth = 0;//记录上一层的宽度
	int nTempLastLevelWidth = 0;
	int nCurLevelWidth = 0;//记录当前层的宽度
	int nWidth = 1;//二叉树的宽度
        queue<BTNode *> myQueue;
	myQueue.push(pRoot);//将根节点入队列
	nLastLevelWidth = 1;	
	BTNode *pCur = NULL;

	while (!myQueue.empty())//队列不空
	{
		nTempLastLevelWidth = nLastLevelWidth;
		while (nTempLastLevelWidth != 0)
		{
            pCur = myQueue.front();//取出队列头元素
			myQueue.pop();//将队列头元素出对

			if (pCur->m_left != NULL)
			{
				myQueue.push(pCur->m_left);
			}

			if (pCur->m_right != NULL)
			{
				myQueue.push(pCur->m_right);
			}

			nTempLastLevelWidth--;
		}

		nCurLevelWidth = myQueue.size();
		nWidth = nCurLevelWidth > nWidth ? nCurLevelWidth : nWidth;
		nLastLevelWidth = nCurLevelWidth;
	}

	return nWidth;
}

int _tmain(int argc, _TCHAR* argv[])
{
	BTNode *pRoot = NULL;	
	CreatBTree(pRoot);
    cout << "二叉树的深度为:" << GetDepth(pRoot) << endl;
	cout << "二叉树的宽度为:" << GetWidth(pRoot) << endl;	
	system("pause");
	return 0;
}

运行结果:


以下是不同实现方式下二叉树最大宽度的主函数示例。 #### 方式一:使用循环深度计算每层宽度 ```c #include <stdio.h> #include <stdlib.h> // 假设的二叉树节点结构体 typedef struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; } BT; // 树的深度 int Depth(BT *root) { if (root == NULL) return 0; int leftDepth = Depth(root->left); int rightDepth = Depth(root->right); return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1; } // 某一层的宽度 int LevelWidth(BT *root, int level) { if (root == NULL) return 0; if (level == 1) return 1; return LevelWidth(root->left, level - 1) + LevelWidth(root->right, level - 1); } // 二叉树的最大宽度 int Width(BT *root) { int width = 0, i; int w[20]; for (i = 0; i < 20; i++) w[i] = 0; if (!root) width = 0; else { for (i = 0; i <= Depth(root); i++) w[i] = LevelWidth(root, i + 1); } i = 0; while (w[i]) { if (w[i] > width) width = w[i]; i++; } return width; } // 创建节点 BT* createNode(int data) { BT* newNode = (BT*)malloc(sizeof(BT)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } int main() { // 创建二叉树的节点 BT* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); root->right->right = createNode(6); int maxWidth = Width(root); printf("二叉树的最大宽度是: %d\n", maxWidth); return 0; } ``` #### 方式二:使用递归深度数组记录每层节点数 ```c++ #include <iostream> #include <vector> using namespace std; // 假设的二叉树节点结构体 struct Tree { int data; Tree* left; Tree* right; Tree(int val) : data(val), left(nullptr), right(nullptr) {} }; // 二叉树的最大宽度 void Treewidth(Tree* T, int& depth, vector<int>& width) { depth++; if (T == nullptr) { depth--; return; } if (depth >= width.size()) { width.resize(depth + 1, 0); } width[depth]++; Treewidth(T->left, depth, width); Treewidth(T->right, depth, width); depth--; } int main() { // 创建二叉树的节点 Tree* root = new Tree(1); root->left = new Tree(2); root->right = new Tree(3); root->left->left = new Tree(4); root->left->right = new Tree(5); root->right->right = new Tree(6); int depth = -1; vector<int> width; Treewidth(root, depth, width); int maxWidth = 0; for (int w : width) { if (w > maxWidth) maxWidth = w; } cout << "二叉树的最大宽度是: " << maxWidth << endl; return 0; } ```
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值