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.

这道题让我们求一个二叉树每层的平均值,那么一看就是要进行层序遍历了,直接上queue啊,如果熟悉层序遍历的方法,那么这题就没有什么难度了,直接将每层的值累计加起来,除以该层的结点个数,存入结果res中即可,参见代码如下:

解法一:

class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
if (!root) return {};
vector<double> res;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
int n = q.size();
double sum = ;
for (int i = ; i < n; ++i) {
TreeNode *t = q.front(); q.pop();
sum += t->val;
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
res.push_back(sum / n);
}
return res;
}
};

下面这种方法虽然是利用的递归形式的先序遍历,但是其根据判断当前层数level跟结果res中已经初始化的层数之间的关系对比,能把当前结点值累计到正确的位置,而且该层的结点数也自增1,这样我们分别求了两个数组,一个数组保存了每行的所有结点值,另一个保存了每行结点的个数,这样对应位相除就是我们要求的结果了,参见代码如下:

解法二:

class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res, cnt;
helper(root, , cnt, res);
for (int i = ; i < res.size(); ++i) {
res[i] /= cnt[i];
}
return res;
}
void helper(TreeNode* node, int level, vector<double>& cnt, vector<double>& res) {
if (!node) return;
if (res.size() <= level) {
res.push_back();
cnt.push_back();
}
res[level] += node->val;
++cnt[level];
helper(node->left, level + , cnt, res);
helper(node->right, level + , cnt, res);
}
};

类似题目:

Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal

参考资料:

https://discuss.leetcode.com/topic/95567/java-solution-using-dfs-with-full-comments

 

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Average of Levels in Binary Tree 二叉树的层平均值的更多相关文章

  1. LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)

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

  2. [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 ...

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

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

  4. 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. Python3解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 ...

  6. LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

    637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...

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

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

  8. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  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. java排序算法(二):直接选择排序

    java排序算法(二) 直接选择排序 直接选择排序排序的基本操作就是每一趟从待排序的数据元素中选出最小的(或最大的)一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完,它需要经过n- ...

  2. cookie session的共同点和区别

    由于HTTP协议是无状态的,在WEB系统中,怎么识别请求来自于哪里呢?是哪一个用户发起的请求呢? 为了解决这一个问题, HTTP协议引入了cookie和session这两个概念 cookie是服务器传 ...

  3. MySQL之数据库和表的基本操作(建立表、删除表、向表中添加字段)

    介绍关于数据库和表的一些基本操作 添加字段.给字段添加注释 ); ) COMMENT '统一社会信用代码录入单位'; ,) 更改字段类型 ,) COMMENT '一头签收,@0或空不用,1必须'; 有 ...

  4. B-day5

    1.昨天的困难,今天解决的进度,以及明天要做的事情 昨天的困难:昨天虽然完成了风险数据的图表统计,但是界面风格仍然不太满意,还在抓紧调试中:还有登录页的背景图,在想应该如何设计, 什么样的风格才好. ...

  5. "未找到应用程序的“aps-environment”的权利字符串"

    1.先生成App ID,在去Provisioning里面生成新的Profile 2.删除Xcode里面原来的push profile(如果没有就不用删除)再次双击新下载的profile(mobilep ...

  6. pandas 数据分析使用

    https://github.com/Erick-LONG/data_analysis/blob/master/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%20%E9%8 ...

  7. 关于Java的异常

    异常机制概述 异常机制是指当程序出现错误后,程序如何处理.具体来说,异常机制提供了程序退出的安全通道.当出现错误后,程序执行的流程发生改变,程序的控制权转移到异常处理器. 异常处理的流程 当程序中抛出 ...

  8. Struts2之Struts2的下载与安装

    Struts2的下载 登陆struts的官网 下载Full Distribution这个选项的struts2的包. 这是Struts2的完整版,里面包括Struts2的实例应用,空实例应用,核心库,源 ...

  9. EL表达式 与 servlvet3.0的新规范

    EL表达式 EL表达式 是一种简化的数据访问方式,是对jsp脚本的简化  . 如我们在一个页面中需要输出session的保存的一个值: <%  out.println(session.getAt ...

  10. Scala 快速入门

     Scalable 编程语言 纯正的的面向对象语言 函数式编程语言 无缝的java互操作 scala之父 Martin Odersky 1. 函数式编程 函数式编程(functional progr ...