class Solution {
public:
//对每个结点进行编号即可
int widthOfBinaryTree(TreeNode* root) {
if (root == NULL)return 0;
queue<pair<TreeNode*, unsigned long long>>myqueue;
int res = 1;
myqueue.push({ root,1 });
while (!myqueue.empty())
{
int len = myqueue.size();
res = max(res, int(myqueue.back().second - myqueue.front().second + 1));
for (int i = 0; i < len; i++)
{
TreeNode*temp = myqueue.front().first;
unsigned long long pos = myqueue.front().second;
myqueue.pop();
if (temp->left != NULL)
myqueue.push({ temp->left,2 * pos });
if (temp->right != NULL)
myqueue.push({ temp->right,2 * pos + 1 });
}
}
return res;
}
};