题目:

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. DevExpress10、RichEditControl

    1.概述 传统.NET界面也有一个RichTextBox控件,一个富文本控件,可存储图片文字,有自己的文件格式RTF. 在DevExpress控件组里面也有一个同等的控件,RichEditContro ...

  2. [python]python官方原版编码规范路径

    1.进入python官方主页:https://www.python.org/ 2.按如下图进入PEP Index ​ 3.选择第8个,即为python的规范 ​

  3. [eclipse]添加python默认模板,在首行添加编码方式(# -*- coding: utf-8 -*-)

    1.从eclipse的windows->preference 2.参照如下图,添加指定的utf-8编码方式

  4. SAP跟踪前台操作导致的后台查询语句

    SAP跟踪前台操作导致的后台查询语句,通过这个可以查看前台对应了后台的数据库表,然后可以通过se11查看表内容,也可以删除表内容. 在sap升级的时候,首先需要拷贝正式的sap系统,然后将拷贝的系统中 ...

  5. tomcat 使用 cronolog 切割日志

    1. 下载 cronolog 软件 wget http://cronolog.org/download/cronolog-1.6.2.tar.gz cronolog-.tar.gz tar zxvf ...

  6. Python2.7-zipfile

    zipfile模块,提供了基本操作后缀为“zip”的文件的接口,一般使用 ZipFile 类完成操作 1.模块方法 zipfile.is_zipfile(filename):判断 filename 是 ...

  7. CentOS7+ anaconda3 + Python-3.6 + tensorflow-cpu-1.5安装和配置

    CentOS7+ anaconda3 + Python-3.6 + tensorflow-cpu-1.5安装和配置 ========================================== ...

  8. jqgrid 主键列的设定

    1.如果需要对jqgrid表格数据有互动操作,需要设定主键列. 2.主键列的作用为:在进行jqgrid表格数据交互(编辑.新增.删除行)时,是通过主键列的值来作为引导值来的. 3.注意:不要给一个jq ...

  9. HDU2552 三足鼎立 【数学推理】

    三足鼎立 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. Kafka 集群部署

    kafka是一个分布式消息队列,需要依赖ZooKeeper,请先安装好zk集群 kafka安装包解压 $ -0.9.0.1.tgz $ -0.9.0.1 /usr/kafka $ cd /usr/ka ...