637. Average of Levels in Binary Tree 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021class Solution {public: vector<double> averageOfLevels(TreeNode* root) { vector<double> ret; if(!root) return ret; queue<TreeNode *> q({root}); while(!q.empty()){ int size = q.size(); double avg = 0; for(int i=0;i<size;++i){ TreeNode *p = q.front(); q.pop(); avg+=p->val; if(p->left) q.push(p->left); if(p->right) q.push(p->right); } ret.push_back(avg/size); } return ret; }};