blob: a18d38d6779eccbdc1806911dcdef7525c1b84c7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include "leetcode.h"
class Solution {
public:
vector<double> averageOfLevels(TreeNode *root) {
vector<double> ans;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
double tmp = q.size(), sum = 0;
for (int i = 0; i < tmp; i++) {
auto tt = q.front();
q.pop();
sum += (tt->val);
if (tt->left)
q.push(tt->left);
if (tt->right)
q.push(tt->right);
}
ans.push_back(sum / tmp);
}
return ans;
}
};
|