- Leetcode PHP题解--D56 637. Average
- Leetcode 637 Average of Levels i
- 7.8-Contest 40-小结
- 637. Average of Levels in Binary
- 637. Average of Levels in Binary
- 637. Average of Levels in Binary
- 637. Average of Levels in Binary
- 637. Average of Levels in Binary
- 637. Average of Levels in Binary
- 【0.5对】 Average of Levels in Bina
Average of Levels in Binary Tree
[思路]:获得树每一层的平均值,使用广度优先搜索。
那么使用方法:
- 广度优先
- deque
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res;
deque<TreeNode*> q;
q.push_back(root);
while(! q.empty()){
double temp = 0;
int s = q.size();
for(int i=0; i<s;i++){
TreeNode* t=q.front();
q.pop_front();
if(t->left) q.push_back(t->left);
if(t->right)q.push_back(t->right);
temp+= t->val;
}
res.push_back(static_cast<double>(temp)/s);
}
return res;
}






网友评论