class Solution {
public:
struct node {
TreeNode* root;
long double site;//用个样例血坑必须扩大范围不然过不了
};
node start,end,tmp,temp;
queue<node> que;long double curLayer=1,index=1,max_len=0;
int widthOfBinaryTree(TreeNode* root) {
start.root = root;start.site = 1;
que.push(start);
while(!que.empty()) {
tmp = que.front();que.pop();
if(tmp.root->left) {
temp.root = tmp.root->left;temp.site = 2*tmp.site;
que.push(temp);
}
if(tmp.root->right) {
temp.root = tmp.root->right;temp.site = 2*tmp.site+1;
que.push(temp);
}
curLayer--;
if(curLayer==0) {
curLayer = que.size();
max_len = max(max_len,tmp.site-start.site+1);
start = que.front();
}
}
return max_len;
}
};
LeetCode:二叉树最大宽度
最新推荐文章于 2023-05-14 16:02:02 发布