PTA 求二叉树的宽度

typedef struct TreeNode *BinTree;
struct TreeNode
{
   int Key;
   BinTree  Left;
   BinTree  Right;
};

int Width( BinTree T )
{
   BinTree  p;
   Queue Q;//队列储存树的结点
   int Last, temp_width, max_width;

   temp_width = max_width = 0;
   Q = CreateQueue(MaxElements);
   Last = Queue_rear(Q);//Last 其实存的是每层最后一个元素的位置
   if ( T == NULL) return 0;
   else {
      Enqueue(T, Q);//根节点进队
      while (!IsEmpty(Q)) {
         p = Front_Dequeue(Q);  //头结点出队        
		temp_width++	(3); 
         if ( p->Left != NULL )  Enqueue(p->Left, Q);//左子树的根结点进队
         
if(p->Right!=NULL) Enqueue(p->Right,Q)// (3分);  
         if ( Queue_front(Q) > Last ) {//这个if很有意思,用来判断当前层是否遍历完
            Last = Queue_rear(Q);
            if ( temp_width > max_width ) max_width = temp_width;
            temp_width=0 (3);//比较完一层后  temp_width清0
         } /* end-if */
      } /* end-while */
      return  max_width;
   } /* end-else */
} 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值