【树】leetcode_637_二叉树的层平均值
程序员文章站
2022-07-14 18:04:53
...
【树】leetcode_637_二叉树的层平均值
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
TreeNode *cur = root;
vector<double> result;
if (root == nullptr) return {};
queue<TreeNode*> q_node;
q_node.push(root);
while(!q_node.empty()){
int size = q_node.size();
double re = 0;
for(int i = 0; i<size; i++){
cur = q_node.front();
q_node.pop();
re += cur->val;
if (cur->left){
q_node.push(cur->left);
}
if (cur->right){
q_node.push(cur->right);
}
}
cout<<re;
result.push_back(re/double(size));
}
return result;
}
};
上一篇: [剑指offfer] 从上到下打印二叉树 II (C++解法)
下一篇: 【C#】DAY4