leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0637.cpp (663B)


      1 class Solution {
      2   public:
      3     vector<double> averageOfLevels(TreeNode *root) {
      4         if (!root) return {};
      5 
      6         vector<double> res;
      7         queue<TreeNode *> q;
      8 
      9         q.push(root);
     10         for (int lvl = 0; !q.empty(); lvl++) {
     11             int cnt = 0;
     12             double sum = 0;
     13             for (int t = q.size(); t > 0; t--) {
     14                 TreeNode *root = q.front();
     15                 q.pop();
     16                 sum += root->val;
     17                 cnt++;
     18                 if (root->left) q.push(root->left);
     19                 if (root->right) q.push(root->right);
     20             }
     21             res.push_back(sum / cnt);
     22         }
     23         return res;
     24     }
     25 }: