题目:

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:

Input:
3
/ \
9 20
/ \
15 7
Output: [3, 14.5, 11]
Explanation:
The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].

Note:

  1. The range of node's value is in the range of 32-bit signed integer.

分析:

题目很好理解,就是求二叉树每一层的平均值。

我们可以创建一个用来存储每层节点值和的数组,和一个存储每层节点个数的数组,通过递归的方式来求二叉树的层平均值,注意在求当前层时要判断层数是否超过了数组的大小,一旦超过,就要扩大数组,一遍存储当前层的信息。

程序:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
getAverage(root, res, );
for(int i = ; i < res.size(); ++i){
res[i] /= nums[i];
}
return res;
}
void getAverage(TreeNode* root, vector<double> &res, int level){
if(root == nullptr) return;
if(level >= res.size()){
res.push_back();
nums.push_back();
}
res[level] += root->val;
nums[level]++;
getAverage(root->left, res, level+);
getAverage(root->right, res, level+);
}
private:
vector<double> res;
vector<int> nums;
};

LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)的更多相关文章

  1. [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  2. [LeetCode] Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  3. Leetcode637.Average of Levels in Binary Tree二叉树的层平均值

    给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...

  4. LeetCode 637 Average of Levels in Binary Tree 解题报告

    题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  5. LeetCode - 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  6. LeetCode 637. Average of Levels in Binary Tree(层序遍历)

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  7. 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值

    [抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  8. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  9. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

随机推荐

  1. Hadoop HBase概念学习系列之HBase里的Zookeeper(二十一)

    这个,很简单,但凡是略懂大数据的,就很清楚,不多说,直接上图.

  2. 【CSS】Sass理解

    原文在 https://github.com/zhongxia245/blog , 欢迎 star! Sass理解 时间:2016-09-24 22:56:12 作者:zhongxia 这里就不讲解S ...

  3. 团队作业8--测试与发布(Beta阶段)

    展示博客 一.项目成员: 张慧敏(组长)201421122032 苏晓薇(组员)201421031033 欧阳时康(组员)201421122050 团队仓库: https://git.coding.n ...

  4. sphinx编译安装

    install sphinx wget http://sphinxsearch.com/files/sphinx-2.0.8-release.tar.gz tar zxvf sphinx--relea ...

  5. BZOJ 1834 网络扩容 最大流+最小费用流

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1834 题目大意: 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是 ...

  6. C借函数指针构造映射

    这是候老师的<深入浅出 MFC>中C借函数指针构造映射截图,可以看到MFC们的映射思想:

  7. python第二十八课——编码小常识

    2.内存和硬盘: 内存:计算机硬件组成部分之一,它是一个容器,用来存储数据:处理数据速度快, 存储数据量小:断电死机数据会丢失,短暂性存储数据 硬盘:计算机硬件组成部分之一,它是一个容器,用来存储数据 ...

  8. Win8下IIS的安装和站点的公布

    版权声明:本文为博主原创文章,不经博主同意注明链接就可以转载. https://blog.csdn.net/Senior_lee/article/details/32939411         之前 ...

  9. OpenCV——掩膜(又称掩码)mask的原理和作用

    一.什么是掩模mask OpenCV中很多函数都带有一个mask参数,mask被称为掩模.图像掩模一般用来对处理的图像(全部或者局部)进行遮挡,来控制图像处理的区域或处理过程. 二.掩模原理 掩模一般 ...

  10. haproxy [WARNING] 312/111530 (17395) : config : 'option forwardfor' ignored for frontend 'harbor_login' as it requires HTTP mode.

    1.经过调查, 2down voteaccepted x-forwarded-for is an HTTP header field, so has nothing to do with the tr ...