9月12号leetcodeme每日一题

思考过程

     这道题就没有什么好说的,经典层序遍历,记录每层的一个size值即可。没有什么价值可言…(就随便贴一下代码好了233)

代码

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

总结

     时间复杂度感觉就O(n),n为树中所有节点的数量,今天的博客就先水一水,看楼教主打比赛去了。

9月12号leetcodeme每日一题

http://woshizs.com/2020/09/11/leetcode-0912/

作者

Jason Heywood

发布于

2020-09-12

更新于

2020-10-14

许可协议