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 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].
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
BFS写不熟
[一句话思路]:
(3先生)先加头、先判Empty、先取长度
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 没概念:BFS中的for循环是针对当前这一层的操作,add的元素都属于下一层
- 二叉树层遍历,q中存的是节点,记住就行了
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
BFS里既然取了长度,就用在for循环中
[复杂度]:Time complexity: O(n) Space complexity: O(n)
所有点放进去一次,拿出来一次,最终还是n
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
q.offer(root);
//isEmpty()
while (! q.isEmpty()) {
//get length
int n = q.size();
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
//corner case
List<Double> ans = new ArrayList<Double>();
Queue<TreeNode> q = new LinkedList<TreeNode>(); if (root == null) {
return ans;
}
//bfs
//offer root
q.offer(root);
//isEmpty()
while (! q.isEmpty()) {
//get length
int n = q.size();
double sum = 0.0;
for (int i = 0; i < n; i++) {
TreeNode node = q.poll();
sum += node.val;
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
ans.add(sum / n);
}
//return
return ans;
}
}
637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值的更多相关文章
- 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 ...
- [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 ...
- 【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 ...
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 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 ...
- [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 of an ...
- 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 ...
- 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 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 ...
随机推荐
- The SDK platform-tools version ((21)) is too old to check APIs compiled with API 23
android studio是个坑爹的工具,每次打开文件头都出现如上错误提示. 解决方法: Update your android sdk platform-tools to the revision ...
- 基于IAR和STM32的uCOS-II移植
网上基于MDK的移植数不胜数,但是基于IAR的移植几乎没有,因为官方的例程就是基于IAR的,所以移植起来很简单,没人介绍,但还是得小心谨慎,一不小心就出错,对于新手来说,查找错误可不是那么容易的.IA ...
- (转)Inno Setup入门(十五)——Inno Setup类参考(1)
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250955 nno setup脚本能够支持许多的类,这些类使得安装 ...
- Maven和Gradle的比较
Gradle和Maven都是项目构建工具,但是完全是两个产品,maven应该目前在java企业级开发中占的比重比较大,Gradle是后起之秀,Google的Android Stadio主推的就是Gra ...
- poj 3977 Subset(折半枚举+二进制枚举+二分)
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 5721 Accepted: 1083 Descripti ...
- scrapy_redis 实现多进程配置部分代码
# 启用Redis调度存储请求队列SCHEDULER = "scrapy_redis.scheduler.Scheduler"# 确保所有的爬虫通过Redis去重DUPEFILTE ...
- 这段时间使用MySQL的一些记录
自从Fedora19之后,Linux上的MySQL就被MariaDB所取代,这段文字见如下引用: MySQL was replaced by MariaDB since Fedora 19 (http ...
- ALSA声卡笔记3--ASoC驱动重要结构体关系图
1.ASoC中重要的数据结构之间的关联方式 (1)Kernel-2.6.35-ASoC中各个结构的静态关系 ASoC把声卡实现为一个Platform Device,然后利用Platform_devic ...
- 【UVa】1374 Power Calculus(IDA*)
题目 题目 分析 IDA*大法好,抄了lrj代码. 代码 #include <cstdio> #include <cstring> #include <a ...
- iframe有哪些缺点?
iframe会阻塞主页面的Onload事件: iframe和主页面共享连接池,而浏览器对相同域的连接有限制,所以会影响页面的并行加载.使用iframe之前需要考虑这两个缺点.如果需要使用ifram ...