原题

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. POPUP_GET_VALUES 金额字段不可编辑

    转自:https://blog.csdn.net/huanglin6/article/details/102733845 当在POPUP_GET_VALUES函数中参考的字段是个货币或者金额字段的话, ...

  2. spring security4.1.3配置以及踩过的坑

    https://blog.csdn.net/honghailiang888/article/details/53520557 spring security完全可以作为一个专门的专题来说,有一个专题写 ...

  3. XenServer 根分区空间满的解决办法

    1.清除已经应用的旧补丁文件 删除 /var/patch/ 下的除 applied 之外的所有文件 2.清除旧版的Xen-Tools文件 删除 /opt/xensource/packages/iso/ ...

  4. 【ARM-Linux开发】嵌入式操作系统上的小型数据库移植SQLite

    近段时间在学数据库,因为自身需求,所以注重研究了点嵌入式sqlite数据库,SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品 ...

  5. 【数据库开发】MySQL命令大全

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  6. 最新 小红书java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.小红书等10家互联网公司的校招Offer,因为某些自身原因最终选择了小红书.6.7月主要是做系统复习.项目复盘.LeetCo ...

  7. 深入理解linux内核-内存寻址

    逻辑地址:由一个段和偏移量组成的地址线性地址(虚拟地址):物理地址:CPU的物理地址线相对应的地址32或36位 多处理器系统中每个CPU对应一个GDT 局部线程存储:用于线程内部的各个函数调用都能访问 ...

  8. webstorm对引入的css资源进行提示

  9. Linux由于物理节点故障导致的异常重启-Case1

    问题描述:Linux VM异常重启,需要排查问题原因 排查结果: 查询Messages日志获取到的信息 虚拟机内核版本: Jun :: test01 kernel: Linux version -.e ...

  10. Sql server 中count(1) 与 sum(1) 那个更快?

    上一篇中,简单的说明了下 count() 与 sum() 的区别,虽然count 函数是汇总行数的,不过我汇总行数的时候经常是使用SUM(1) ,那么问题来了,count(1) 与 sum(1)  那 ...