题目:
代码(首刷自解 2024年1月24日):
cpp
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res = {};
if(root == nullptr) return res;
queue<TreeNode*> que;
TreeNode* cur = root;
que.push(cur);
int size = 0;
while (!que.empty()) {
size = que.size();
double digit = size;
long long sum = 0;
while (size--) {
TreeNode* node = que.front();
sum += node->val;
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res.emplace_back(sum/digit);
}
return res;
}
};