求二叉数的深度和宽度

求二叉数的深度和宽度

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

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

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

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

        1.C++

[cpp]  view plain copy 在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 plain copy 在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. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值