码迷,mamicode.com
首页 > 其他好文 > 详细

给定一个二叉树,获取该二叉树的宽度深度

时间:2017-06-01 13:22:05      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:esc   tracking   tac   queue   ++   node   script   head   height   

题目:

Description  

         给定一个二叉树,获取该二叉树的宽度深度。


Prototype
         int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
Input Param 
         head   须要获取深度的二叉树头结点
Output Param 
         pulWidth   宽度
         pulHeight  高度
Return Value
         0          成功

         1          失败或其它异常

分析:使用二叉树的层序遍历,使用队列非常easy的解决这个问题

代码例如以下:

int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
{
	/*在这里实现功能*/
	if(&head==NULL)
		return -1;
	*pulWidth=0;
	*pulHeight=0;
	queue<BiNode*> biStack;
	biStack.push(&head);
	while(!biStack.empty()){
		++(*pulHeight);
		if(biStack.size()>*pulWidth)
			*pulWidth=biStack.size();
		int i=biStack.size();
		while(i>0){
			BiNode * temp=biStack.front();
			biStack.pop();
			if(temp->left!=NULL)
				biStack.push(temp->left);
			if(temp->right!=NULL)
				biStack.push(temp->right);
			i--;
		}
	}
	printf("pulWidth=%d\n pulHeight=%d\n",*pulWidth,*pulHeight);
	return 0;
}


给定一个二叉树,获取该二叉树的宽度深度

标签:esc   tracking   tac   queue   ++   node   script   head   height   

原文地址:http://www.cnblogs.com/zhchoutai/p/6928134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!