原题

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. 【ARTS】01_35_左耳听风-201900708~201900714

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  2. iOS笔试题01

    1. #import 跟#include.@class有什么区别?#import<> 跟 #import”"又什么区别? 1> #import和#include都能完整地包 ...

  3. Azure DevOps的variable group实现array和hashtable参数的传递

    Azure Devops中的variable group建议或者只能(?)添加string类型的value.基于此我们想在variable group实现array或者hashtable的传递的核心思 ...

  4. AWS 存储服务(三)

    目录 AWS S3 业务场景 挑战 解决方案 S3的好处 S3 属性 存储桶 Buckets 对象 Object S3 特性 S3 操作 可用性和持久性 一致性 S3 定价策略 S3高级功能 存储级别 ...

  5. 三节课MINI计划第二周

    任务:完成一份用户反馈的收集,并进行分析 第一步:去你能想到的公开.非公开渠道收集最近90天,至少40条和B站相关的有效用户差评反馈,并根据你对业务的理解分类整理,以表格的形式进行整理,以图片的方式提 ...

  6. eNSP——静态路由的基本配置

    原理: 静态路由是指用户或网络管理员手工配置的路由信息.当网络的拓扑结构或链路状态发生改变时,需要网络管理人员手工修改静态路由信息. 相比于动态路由协议,静态路由无需频繁地交换各自的路由表,配置简单, ...

  7. jira邮箱配置

    系统-邮件-外发邮件

  8. cv2---imread---error

    when I use the cv2.imred() which is absolute path  path = r'C:\\Users\\hp\\Desktop\\常用Python代码\\mycv ...

  9. FAQ and discussed with adam

    1. About permuter index. url:  https://www.youtube.com/watch?v=j789k96g5aQ&list=PL0ZVw5-GryEkGAQ ...

  10. JAVA第09次实验(IO流)

    JAVA第09次实验(IO流) 0.字节流与二进制文件 我的代码 import java.io.DataInputStream; import java.io.DataOutputStream; im ...