题目:

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. easyui学习笔记14-拓展的基本验证规则

    /** * 扩展的基本校验规则, */ $.extend($.fn.validatebox.defaults.rules, { minLength : { // 判断最小长度 validator : ...

  2. PyQt5--GridLayout

    # -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  3. 使用let声明变量的理解

    先看阮大神的[ECMAScript 6 入门]中关于这一部分的描述 var a = []; for (let i = 0; i < 10; i++) { a[i] = function () { ...

  4. apache不能启动LoadModule php5_module modules/ph

    apache不能启动LoadModule php5_module modules/php5apache2.dll的问题 主要是版本问题!!有点不爽!! apache不能启动 加入下面两行,apache ...

  5. curl_init 接口

    <?php// +----------------------------------------------------------------------// | ThinkPHP [ WE ...

  6. MyBatis实战之初步

    关于MyBatis与Hibernate及其JDBC的比较,大家可以参考我的这篇文章:MyBatis+Hibernate+JDBC对比分析 如果觉得这个还不够系统全面,可以自行Google或者百度. 用 ...

  7. Python2.7-collections

    collections 模块主要提供了五种特殊类型容器,此外还提供了许多抽象基类用于检查类的接口 1.Counter 对象,主要用于统计出现次数,是dict的一个子类,用法与形式和 dict 很类似 ...

  8. 解决IDEA因分配内存而引起的卡顿

    解决IDEA分配内存不足引起卡顿的问题 在使用IDEA的过程中,经常会运行一段时间后程序卡顿.一段代码可能要敲很久或者出现死机状态,严重影响代码书写速度.经查阅资料,IDEA的自动分配内存最大只有75 ...

  9. 【转】为什么volatile不能保证原子性而Atomic可以?

    直接上好文链接!!! 为什么volatile不能保证原子性而Atomic可以?

  10. mysql删除关联表数据

    DELETE og,oiFROM order_goods og, order_info oiWHERE oi.order_id = og.order_id and oi.user_id = 6