求二叉数的深度和宽度

本文介绍了如何使用C++和Java实现二叉树的深度和宽度计算,包括求解算法和具体代码实现。

        二叉树的深度:二叉树的根结点所在的层数为1,根结点的孩子结点所在的层数为2,以此下去。深度是指所有结点中最深的结点所在的层数。

        二叉树的宽度:所有深度中含有的最多的子叶数。

        参考文献:http://blog.youkuaiyun.com/htyurencaotang/article/details/12406223#comments

        1.C++

//求二叉树的深度
int GetDepth(tagBiNode *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->left) > GetDepth(pRoot->right) ? 
		(GetDepth(pRoot->left) + 1) : (GetDepth(pRoot->right) + 1);
}

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

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

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

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

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

			nTempLastLevelWidth--;
		}

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

	return nWidth;
}
        2.Java

	public static int getHeight(BiNode head)
	{
		int deep = 0;
		if(head != null)
		{
			int left = getHeight(head.left);
			int right = getHeight(head.right);
			deep = (left>=right)?(left+1):(right+1);
		}
		return deep;
	}
	public static int getWidth(BiNode head)
	{
		if(head == null)
		{
			return 0;
		}
		
		int nWidth = 0;
		int nLastLevelWidth = 0;
		int nTempLastLevelWidth = 0;
		int nCurLevelWidth = 0; 
		
		Queue<BiNode> myQueue = new LinkedList<Demo.BiNode>();
		myQueue.add(head);
		nLastLevelWidth = 1;
		nWidth = 1;
		
		while(!myQueue.isEmpty())
		{
			nTempLastLevelWidth = nLastLevelWidth;
			BiNode tmp = null;
			while(nTempLastLevelWidth != 0)
			{
				tmp = myQueue.peek();
				myQueue.poll();

				if(tmp.left != null)
				{
					myQueue.add(tmp.left);
				}
				if(tmp.right != null)
				{
					myQueue.add(tmp.right);
				}
				nTempLastLevelWidth--;
			}
			
			nCurLevelWidth = myQueue.size();
			nWidth = nCurLevelWidth>nWidth?nCurLevelWidth:nWidth;
			nLastLevelWidth = nCurLevelWidth;
		}
		return nWidth;
	}

以下是不同实现方式下二叉树最大宽度的主函示例。 #### 方式一:使用循环深度计算每层宽度 ```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.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值