当前位置: 首页 > news >正文

dwcc2017做网站教程/app开发定制

dwcc2017做网站教程,app开发定制,石家庄网站制作公司哪家好,wordpress固定链接index.php求二叉数的深度和宽度 分类: 数据结构与算法2014-02-24 17:02 1356人阅读 评论(0) 收藏 举报二叉树的深度:二叉树的根结点所在的层数为1,根结点的孩子结点所在的层数为2,以此下去。深度是指所有结点中最深的结点所在的层数。 二叉…

求二叉数的深度和宽度

分类: 数据结构与算法 1356人阅读 评论(0) 收藏 举报

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

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

        参考文献:http://blog.csdn.net/htyurencaotang/article/details/12406223#comments

        1.C++

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //求二叉树的深度  
  2. int GetDepth(tagBiNode *pRoot)  
  3. {  
  4.     if (pRoot == NULL)  
  5.     {  
  6.         return 0;  
  7.     }  
  8.   
  9.     //  int nLeftLength = GetDepth(pRoot->m_left);  
  10.     //  int nRigthLength = GetDepth(pRoot->m_right);  
  11.     //  return nLeftLength > nRigthLength ? (nLeftLength + 1) : (nRigthLength + 1);  
  12.   
  13.     return GetDepth(pRoot->left) > GetDepth(pRoot->right) ?   
  14.         (GetDepth(pRoot->left) + 1) : (GetDepth(pRoot->right) + 1);  
  15. }  
  16.   
  17. //求二叉树的宽度  
  18. int GetWidth(tagBiNode *pRoot)  
  19. {  
  20.     if (pRoot == NULL)  
  21.     {  
  22.         return 0;  
  23.     }  
  24.   
  25.     int nLastLevelWidth = 0;//记录上一层的宽度  
  26.     int nTempLastLevelWidth = 0;  
  27.     int nCurLevelWidth = 0;//记录当前层的宽度  
  28.     int nWidth = 1;//二叉树的宽度  
  29.     queue<BiNode *> myQueue;  
  30.     myQueue.push(pRoot);//将根节点入队列  
  31.     nLastLevelWidth = 1;      
  32.     tagBiNode *pCur = NULL;  
  33.   
  34.     while (!myQueue.empty())//队列不空  
  35.     {  
  36.         nTempLastLevelWidth = nLastLevelWidth;  
  37.         while (nTempLastLevelWidth != 0)  
  38.         {  
  39.             pCur = myQueue.front();//取出队列头元素  
  40.             myQueue.pop();//将队列头元素出对  
  41.   
  42.             if (pCur->left != NULL)  
  43.             {  
  44.                 myQueue.push(pCur->left);  
  45.             }  
  46.   
  47.             if (pCur->right != NULL)  
  48.             {  
  49.                 myQueue.push(pCur->right);  
  50.             }  
  51.   
  52.             nTempLastLevelWidth--;  
  53.         }  
  54.   
  55.         nCurLevelWidth = myQueue.size();  
  56.         nWidth = nCurLevelWidth > nWidth ? nCurLevelWidth : nWidth;  
  57.         nLastLevelWidth = nCurLevelWidth;  
  58.     }  
  59.   
  60.     return nWidth;  
  61. }  
        2.Java

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static int getHeight(BiNode head)  
  2. {  
  3.     int deep = 0;  
  4.     if(head != null)  
  5.     {  
  6.         int left = getHeight(head.left);  
  7.         int right = getHeight(head.right);  
  8.         deep = (left>=right)?(left+1):(right+1);  
  9.     }  
  10.     return deep;  
  11. }  
  12. public static int getWidth(BiNode head)  
  13. {  
  14.     if(head == null)  
  15.     {  
  16.         return 0;  
  17.     }  
  18.       
  19.     int nWidth = 0;  
  20.     int nLastLevelWidth = 0;  
  21.     int nTempLastLevelWidth = 0;  
  22.     int nCurLevelWidth = 0;   
  23.       
  24.     Queue<BiNode> myQueue = new LinkedList<Demo.BiNode>();  
  25.     myQueue.add(head);  
  26.     nLastLevelWidth = 1;  
  27.     nWidth = 1;  
  28.       
  29.     while(!myQueue.isEmpty())  
  30.     {  
  31.         nTempLastLevelWidth = nLastLevelWidth;  
  32.         BiNode tmp = null;  
  33.         while(nTempLastLevelWidth != 0)  
  34.         {  
  35.             tmp = myQueue.peek();  
  36.             myQueue.poll();  
  37.   
  38.             if(tmp.left != null)  
  39.             {  
  40.                 myQueue.add(tmp.left);  
  41.             }  
  42.             if(tmp.right != null)  
  43.             {  
  44.                 myQueue.add(tmp.right);  
  45.             }  
  46.             nTempLastLevelWidth--;  
  47.         }  
  48.           
  49.         nCurLevelWidth = myQueue.size();  
  50.         nWidth = nCurLevelWidth>nWidth?nCurLevelWidth:nWidth;  
  51.         nLastLevelWidth = nCurLevelWidth;  
  52.     }  
  53.     return nWidth;  
  54. }  
http://www.jmfq.cn/news/5188537.html

相关文章:

  • 苹果cms做网站/公司网站制作需要多少钱
  • 桂林北站到龙脊梯田/百度快速排名优化技术
  • 伍佰亿网站怎样/网页设计教程
  • 最优网络做网站骗/广州抖音推广
  • wordpress 回车换行/谷歌seo代运营
  • 做旅游网站的社会效益可行性/日结app推广联盟
  • 自己做网站必须要学哪些/河南平价的seo整站优化定制
  • 福建有没有网站做一件代发/长沙关键词快速排名
  • 手机网站有免费做的吗/社群营销方案
  • 门户网站什么意思/网络推广怎么赚钱
  • 商城网站制作/人工智能培训班收费标准
  • 做网站关键词/高端网站建设深圳
  • 企业网站备案意义/湖南正规关键词优化报价
  • 做1元夺宝网站挣钱吗/网络策划方案
  • 软件网站的服务器/深圳专门做seo的公司
  • 表格如何给网站做链接地址/百度客户端在哪里打开
  • 做个网站大约多少钱/营销型网站制作公司
  • 做电影方面的网站怎么做/网络舆情管理
  • 娄底企业网站建设制作/b站视频推广
  • 淄博政府网站建设专家/百度客服系统
  • 衢州建筑结构加固哪家好/北京seo优化多少钱
  • 厦门专业网站设计代理/南和网站seo
  • 企业网站的常见类型有什么/西安做网站哪家好
  • 网站做多久才能每日上万/精准ip地址查询工具
  • 网站标签怎么设置/武汉seo全网营销
  • 沈阳电商网站建设/百度一下照片识别
  • 陇南网站网站建设/安全又舒适的避孕方法有哪些
  • 鳌江网站建设/正规考证培训机构
  • 专做网站的公司/软文文案
  • 门户网站建设推荐/网络推广方法怎么样