662. Maximum Width of Binary Tree

本文介绍了一种求解二叉树最大宽度的算法实现,通过遍历每一层的节点来确定最大宽度,并提供了具体的代码示例。适用于二叉树结构的宽度计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.

The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.

 

Example 1:

Input: 

           1
         /   \
        3     2
       / \     \  
      5   3     9 

Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

Example 2:

Input: 

          1
         /  
        3    
       / \       
      5   3     

Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).

Example 3:

Input: 

          1
         / \
        3   2 
       /        
      5      

Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).

Example 4:

Input: 

          1
         / \
        3   2
       /     \  
      5       9 
     /         \
    6           7
Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).


Note: Answer will in the range of 32-bit signed integer.

 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        if(root == NULL)
            return 0;
        queue<TreeNode *> real_queue;
        queue<int> virtual_queue;
        
        //利用实队列和虚队列 实队列保存节点信息 虚队列保存节点位置信息 LeetCode中的discuss利用递归方法 更是巧妙
        int lastLayerSum = 1;
        int currentLayerSum = 0;
        int MaxSum = 0;
        bool first = true;         // 是否为每层的第一个节点
        int firstflag = 0;         // 表示每层第一个节点的编号
        
        real_queue.push(root);
        virtual_queue.push(1);
        
        while(!real_queue.empty())
        {
            currentLayerSum = 0;
            first = true;
            while(lastLayerSum)
            {
                TreeNode * node = real_queue.front();
                real_queue.pop();
                int flag = virtual_queue.front();
                virtual_queue.pop();
                
                if(first)
                {
                    firstflag = flag;
                    first = false;
                }
                if ((flag-firstflag+1)>MaxSum)
                    MaxSum = flag-firstflag+1;
               
                if(node->left) 
                {
                    real_queue.push(node->left);
                    virtual_queue.push(2*flag);
                    currentLayerSum ++ ;
                }
                
                if(node->right) 
                {
                    real_queue.push(node->right);
                    virtual_queue.push(2*flag+1);
                    currentLayerSum ++;
                }
                lastLayerSum--;
            }
            lastLayerSum = currentLayerSum;
        }
        
        return MaxSum;
    }
};

 

 

leetcode例子:

 int widthOfBinaryTree(TreeNode* root) {
        return dfs(root, 0, 1, vector<pair<int, int>>() = {});
    }
    
    int dfs(TreeNode* root, int level, int order, vector<pair<int, int>>& vec){
        if(root == NULL)return 0;
        if(vec.size() == level)vec.push_back({order, order});
        else vec[level].second = order;
        return max({vec[level].second - vec[level].first + 1, dfs(root->left, level + 1, 2*order, vec), dfs(root->right, level + 1, 2*order + 1, vec)});
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值