662. Maximum Width of Binary Tree

来源:互联网 发布:excel取消数据验证 编辑:程序博客网 时间:2024/06/05 18:41

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: 4Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

Example 2:

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

Example 3:

Input:           1         / \        3   2        /              5      Output: 2Explanation: The maximum width existing in the second level with the length 2 (3,2).

Example 4:

Input:           1         / \        3   2       /     \        5       9      /         \    6           7Output: 8Explanation: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)});    }


阅读全文
0 0
原创粉丝点击