原题

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:

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

解析

给一颗树,求每一层的平均数,返回一个List

思路

我的想法是将一层的所有节点加入到一个list中,计算list的平均数,并递归计算list中节点的子节点(会构建多个list,空间利用较多)

最优解是使用了广度优先算法,利用queue将一层的节点先全部入列,计算平均数,并将其子节点入列,每层循环用n记录当前queue中的节点数,作为内层循环的循环次数

我的解法

public List<Double> averageOfLevels(TreeNode root) {
List<Double> avg = new ArrayList<>();
if (root == null) {
return null;
}
List<TreeNode> list = new ArrayList<TreeNode>() {
{
add(root);
}
};
getAvg(list, avg);
return avg;
} private void getAvg(List<TreeNode> list, List<Double> avg) {
if (list == null || list.size() <= 0) {
return;
}
List<TreeNode> childTreeNodeList = new ArrayList<>();
Double sum = 0D;
for (TreeNode t : list) {
sum += t.val;
if (t.left != null) {
childTreeNodeList.add(t.left);
}
if (t.right != null) {
childTreeNodeList.add(t.right);
}
}
avg.add(sum / list.size());
getAvg(childTreeNodeList, avg);
}

最优解

public List<Double> averageOfLevelsBFS(TreeNode root) {
if (root == null) {
return null;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>() {
{
add(root);
}
};
List<Double> avg = new ArrayList<>();
while (!queue.isEmpty()) {
//queue的长度为当前行的元素数
int n = queue.size();
Double sum = 0D;
for (int i = 0; i < n; i++) {
TreeNode t = queue.poll();
sum += t.val;
if (t.left != null) {
queue.offer(t.left);
}
if (t.right != null) {
queue.offer(t.right);
}
}
avg.add(sum / n);
}
return avg;
}

【leetcode】637. Average of Levels in Binary Tree的更多相关文章

  1. 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

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

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

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

  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 of an ...

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

  6. 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 ...

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

  8. [LeetCode&Python] Problem 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 ...

  9. 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 ...

随机推荐

  1. 通过本地Agent监控Azure sql database

    背景: 虽然Azure sql database有DMVs可以查看DTU等使用情况,但记录有时间限制,不会一直保留.为了更好监控Azure_sql_database上各个库的DTU使用情况.数据库磁盘 ...

  2. videojs改变音量大小

    <audio id=example-video preload="auto" class="video-js vjs-default-skin" type ...

  3. videojs调整音频播放语速

    参考来源: https://stackoverflow.com/questions/19112255/change-the-video-playback-speed-using-video-js 以下 ...

  4. Apache配置优化之开启GZip传输

    1.确保apache已经编译的模块里有mod_deflate模块 2.确保apache的配置文件里引入了压缩的模块 3.确保要开启Gzip压缩的虚拟主机配置里有如下配置,并重启apache服务:如果要 ...

  5. 项目中学习ReactiveCocoa的使用方法

    一.注册控制器 控制器上的一个属性 @property (weak, nonatomic) IBOutlet UIBarButtonItem *signInBtn; 在 viewDidLoad 方法中 ...

  6. Vue-cli中的安装方法

    vue-cli脚手架模板是基于node下的npm来完成安装的所以首先需要安装node 1.安装node,vue运行需要基于npm一定的版本,所以首先升级npm到最新的版本,而在安装的过程中个人比较喜欢 ...

  7. discuz 访问 Discuz! Database Error(2002) notconnect

    网站四五年了,今天突然访问,提示这个错误,在通过阿里云后台查看 CPU 一直处于100%,通过xshell 登录 OK 先看下数据库 不能重启,原来是数据库日志被写满了. /var/log/httpd ...

  8. 微服务之Polly熔断策略

    NET Core 微服务之Polly熔断策略 紧接着上一篇说,咱们继续介绍Polly这个类库 熔断策略(Circuit-breaker) 如果调用某个目标服务出现过多超时.异常等情况,可以采取一定时间 ...

  9. Net中实现HTML生成图片或PDF

    Net中实现HTML生成图片或PDF的几种方式 前段时间由于项目上的需求,要在.Net平台下实现把HTML内容生成图片或PDF文件的功能,特意在网上研究了几种方案,这里记录一下以备日后再次使用.当时想 ...

  10. 17-js观察者模式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...