原题

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. iOS 百度地图报私有api的解决方案

    1.Build Settings-->搜索other linker Flags-->将other linker Flags设置为-objc 2.用2.1.1的版本的百度地图 3.换高德地图

  2. 基于OpenAM系列的SSO----基础

    基于OpenAM系列的SSO----基础   OpenAM简介:OpenAM是一个开源的访问管理.授权服务平台.由ForegeRock公司发起.OpenAM前身为OpenSSO,由SUN公司创建,现在 ...

  3. java构造方法前加void有什么作用

    构造方法前面是没有任何返回符,不能加任何返回类型,包括void.一个构造方法一旦加了void,那么这个就不是构造方法了,变成了一个普通的方法.至于你程序出现的情况,是这样的.任何一个Java类,如果你 ...

  4. python之bytes和string相互转换

    来源:https://www.cnblogs.com/skiler/p/6687337.html 1.bytes主要是给计算机看的,string主要是给人看的 2.中间有个桥梁就是编码规则,现在大趋势 ...

  5. python:python2与python3共存时,pip冲突,提示Fatal error in launcher: Unable to create process using '"d:\python27\python2.exe" "D:\Python27\Scripts\pip2.exe" '

    问题背景: 机器上同时装了python2.和python3后,导致只能用pip3了,使用pip2时提示:Fatal error in launcher: Unable to create proces ...

  6. charles 工具菜单总结

    本文参考:charles 工具菜单总结 主要是下面的功能,具体可以点击对应菜单查看 工具菜单总结 禁用缓存 禁用Cookies 远程映射到URL地址 映射到本地 重写工具 黑名单 白名单 DNS欺骗 ...

  7. 100/200/400GE高速以太网:Autoneg & Link Training 自适应及链路学习相关姿势介绍

    2019-10-31 08:29:22 先写个目录,陆续补齐 PAM4模式下50GE,100GE,200GE,400GE以太网为什么需要AN & LT功能?AN .Autoneg自适应功能介绍 ...

  8. 【数据库开发】Redis消息通知

    消息通知 任务队列 使用任务队列的好处 松耦合.生产者和消费者无需知道彼此的实现细节,只需要约定好任务的描述格式.这使得生产者和消费者可以由不同的团队使用不同的编程语言编写 易于扩展.消费者可以有多个 ...

  9. Spring 商品分类

    实体商品示例代码 package cn.maxhou.entity; import java.io.Serializable; import java.math.BigDecimal; import ...

  10. 综合练习2 设置访问权限,Easy-IP访问外网,内外网访问

    实验拓扑图: 实验要求: 1.pc.路由.交换基本配置,vlan间路由互通. 2.vlan20.vlan30可以访问FTP,VLAN10不允许访问FTP. 3.AR1通过easy-ip方式实现私网地址 ...