zoukankan      html  css  js  c++  java
  • 给定一个二叉树,获取该二叉树的宽度深度

    题目:

    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
     pulHeight=%d
    ",*pulWidth,*pulHeight);
    	return 0;
    }
    


  • 相关阅读:
    移动端WEB开发真机测试
    前端自学路线之js篇
    学习提高你CSS技术的法则
    day-5元组专区
    day5-列表专区
    day4-字符串专区
    day2-day3基本数据类型专区
    day1-习题
    day1-python条件语句和基本数据类型
    day1-python初识以及变量
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6928134.html
Copyright © 2011-2022 走看看