leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0637.cpp (663B)
0 class Solution {
1 public:
2 vector<double> averageOfLevels(TreeNode *root) {
3 if (!root) return {};
5 vector<double> res;
6 queue<TreeNode *> q;
8 q.push(root);
9 for (int lvl = 0; !q.empty(); lvl++) {
10 int cnt = 0;
11 double sum = 0;
12 for (int t = q.size(); t > 0; t--) {
13 TreeNode *root = q.front();
14 q.pop();
15 sum += root->val;
16 cnt++;
17 if (root->left) q.push(root->left);
18 if (root->right) q.push(root->right);
19 }
20 res.push_back(sum / cnt);
21 }
22 return res;
23 }
24 }: