二叉树最大宽度
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190830181439147.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW9ob3V0YW8=,size_16,color_FFFFFF,t_70)
/**
* 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) {
TreeNode* node = root;
deque<pair<TreeNode*, int>> de;
int cur = 1;
de.push_back(make_pair(root, cur));
int max_len = 1;
while (!de.empty())
{
if(de.size() == 1)
de.front().second = 1;
max_len = max(max_len, (de[de.size() - 1].second - de[0].second)+1);
for (int i = de.size() - 1; i >= 0; --i)
{
auto temp = de.front();
de.pop_front();
auto t_node = temp.first;
auto cur_count = temp.second;
if (t_node->left)
{
de.push_back(make_pair(t_node->left, cur_count * 2 ));
}
if (t_node->right)
{
de.push_back(make_pair(t_node->right, cur_count * 2 + 1));
}
}
}
return max_len;
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190830181502563.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW9ob3V0YW8=,size_16,color_FFFFFF,t_70)