Average of Levels in Binary Tree LeetCode solution in C++

1
class Solution {
2
public:
3
vector<double> averageOfLevels(TreeNode *root) {
4
queue<TreeNode *> q;
5
q.push(root);
6
vector<double> ans;
7
while (q.size()) {
8
double qlen = q.size(), row = 0;
9
for (int i = 0; i < qlen; i++) {
10
TreeNode *curr = q.front();
11
q.pop();
12
row += curr->val;
13
if (curr->left) q.push(curr->left);
14
if (curr->right) q.push(curr->right);
15
}
16
ans.push_back(row / qlen);
17
}
18
return ans;
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0